C# DatagridView 不显示错误图标或错误文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/291475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DatagridView Not Displaying the error icon or error text?
提问by
I have a win form (c#) with a datagridview. I set the grid's datasource to a datatable.
我有一个带有 datagridview 的 win 表单(c#)。我将网格的数据源设置为数据表。
The user wants to check if some data in the datatable exists in another source, so we loop through the table comparing rows to the other source and set the rowerror on the datatable to a short message. The datagridview is not showing these errors. The errortext on the datagridviewrows are set, but no error displayed.
用户想要检查数据表中的某些数据是否存在于另一个源中,因此我们循环遍历表,将行与另一个源进行比较,并将数据表上的 rowerror 设置为一条短消息。datagridview 没有显示这些错误。datagridviewrows 上的错误文本已设置,但未显示错误。
Am I just expecting too much for the errors to show and they only show in the context of editing the data in the grid?
我是否只是对显示的错误期望过高,而它们仅在编辑网格中的数据的上下文中显示?
I have been tinkering with this for a day and searched for someone that has posted a simalar issue to no avail - help!
我已经修补了一天并搜索了发布类似问题但无济于事的人 - 帮助!
回答by Robert Wagner
I believe that the errors will only show on editing. What you could do is add a bool column to your DataTable, which drives the display of an image/custom column in the DataGridView, reflecting whether there is an error or not.
我相信错误只会在编辑时显示。您可以做的是在您的 DataTable 中添加一个 bool 列,它驱动 DataGridView 中图像/自定义列的显示,反映是否有错误。
回答by Andrew
Check that AutoSizeRowsMode
is set to DataGridViewAutoSizeRowsMode.None
. I have found that the row Errortext
preview icon is not displayed when AutoSizeRowsMode
is not set to the default of none.
检查AutoSizeRowsMode
是否设置为DataGridViewAutoSizeRowsMode.None
。我发现Errortext
当AutoSizeRowsMode
未设置为默认值 none时,行预览图标不会显示。
DataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None
回答by Eric Schneider
This is a bit late for the original poster, but here what solved it for me...
这对于原始海报来说有点晚了,但是这里为我解决了这个问题......
Check the row height. If it's less than 19it will not draw the icon. Try setting it a bit higher to see if thats the problem.
检查行高。如果小于19,则不会绘制图标。尝试将其设置得更高一些,看看是否是问题所在。
grid.RowTemplate.Height = 22
回答by KenDog
If you set e.Cancel to True the icon does not display. Which does not let the user know that a problem exists on that line.
如果您将 e.Cancel 设置为 True,则不会显示该图标。这不会让用户知道该线路上存在问题。
回答by Julien P
In case someone else is still searching nowadays: The solution that worked for me was to re-assign the (same) DataSource to the DataGridView, and call the Refresh method on the grid after having set the RowError properties.
如果其他人现在仍在搜索:对我有用的解决方案是将(相同的)DataSource 重新分配给 DataGridView,并在设置 RowError 属性后调用网格上的 Refresh 方法。
(VB.Net code:)
(VB.Net 代码:)
myDataGridView.DataSource = myDataSet.Tables(0)
myDataGridView.Refresh()
After doing that, the newly assigned RowError's were finally displayed.
这样做之后,新分配的 RowError 终于显示出来了。
回答by Tim Fortune
The DataGridView
has to be visible at the time the ErrorText
property is set.
本DataGridView
必须是在当时可见的ErrorText
属性设置。
回答by strongline
Send an ESC keystroke will force it to show (at least worked for me)
发送 ESC 按键将强制它显示(至少对我有用)
SendKeys.Send("{ESC}");
回答by esc
Check dgv.ShowRowErrors
property.
检查dgv.ShowRowErrors
财产。
回答by Norman Yuan
One more reason the error icon is not showing is, if the row header size is too small. By default, it is 46. If for some reason you set the row header to a smaller size, such as 30, the error icon will not display.
错误图标未显示的另一个原因是行标题太小。默认情况下,它是 46。如果由于某种原因将行标题设置为较小的大小,例如 30,则不会显示错误图标。
回答by Bolek
I experienced similar issue when validating user input in the
我在验证用户输入时遇到了类似的问题
private void gridGrid_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
handler. The problem was I set e.Cancel=true
in case of invalid input.
处理程序。问题是我e.Cancel=true
在输入无效的情况下设置的。