是否可以使 DataGridView、DataGridColumn、DataGridCell 模仿 Excel 中的特定行为?
技术: Visual Studio .NET 2008
我想知道是否有人知道强制 datagridview 中的列的行为与 Excel 类似的方法。
目前,这就是数据的样子:
A) 123456789.02211
我想看到的是
B) 123,456,789.02
但是,问题是,我希望数据在不使用时看起来像“B”(没有焦点,鼠标不在单元格中)并且我希望数据在使用时看起来像“A”。 (单元格具有焦点,而不是整个列。)
如果我不能执行特定单元格,那也没关系。如果它像“当 dataGridview 具有焦点时,所有数据看起来都像 A 一样广泛,我可以处理这个问题。
有什么想法吗?
Technologies: Visual Studio .NET 2008
I'm wondering if anyone is aware of a way to force a column in a datagridview to behave similarily to Excel.
Currently, this is what the data looks like:
A) 123456789.02211
what I would like to see is
B) 123,456,789.02
However, the catch is, I want the data to look like "B" when not in use (no focus, mouse isn't in cell) and I want the data to look like "A" when in use. (Cell has focus, not the whole column.)
If I can't do the specific cell, that's fine. If it's as broad as "When the dataGridview has focus, all data looks like A, I can deal with that.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要查看 DataGridView 的
CellBeginEdit
和CellEndEdit
事件。CellBeginEdit
事件将允许您设置要编辑的单元格的格式,而CellEndEdit
事件将允许您在编辑完成后重置单元格的格式。像这样:
CellBeginEdit
和CellEndEdit
事件都有 EventArg 属性,您可以使用它们来确定是否(和/或如何)设置单元格格式。我的示例使用e.ColumnIndex
属性来确保我的日期列未格式化。您可能需要不同的格式字符串,但这是您正在寻找的通用方法。
You'll want to take a look at the DataGridView's
CellBeginEdit
andCellEndEdit
events. TheCellBeginEdit
event will allow you to format the cell you're about to edit and theCellEndEdit
event will allow you to reset the cell's format back after editing is complete.Like this:
Both the
CellBeginEdit
and theCellEndEdit
events have EventArg properties that you can use to determine if (and/or how) you want to format your cells. My example uses thee.ColumnIndex
property to ensure that my date column is not formatted.You might need different formatting strings but this is the general approach you're looking for.