How do I prevent a DataGridView from sharing rows?

你说的曾经没有我的故事 提交于 2020-04-16 04:59:26

问题


I've made a custom DataGridViewCell that displays a custom control instead of the cell; but if the DataGridView uses shared rows, then the custom control instance is also shared, so you get strange behaviour (for example, hovering over buttons highlights all the buttons). Also, I can't access the DataGridViewCell.Selected property, so I don't know what colour to paint the row.

How do I prevent a DataGridView from sharing rows? I know I can add the rows using the Rows.Add(object[]) override, but then the first row is still shared (i.e. has index -1) so the problem with colours still applies.

I need to be able to tell the DataGridView not to share a row containing a custom cell. Can that be done with attributes? Can it be done at all?


回答1:


Try to set a Tooltip Text in one cell of the Row

A row cannot be shared in any of the following situations: The row contains a single selected cell that is not in a selected column. The row contains a cell with its ToolTipText or ContextMenuStrip properties set. The row contains a DataGridViewComboBoxCell with its Items property set.

or read

http://msdn.microsoft.com/en-us/library/ha5xt0d9.aspx

for more Information




回答2:


DataGridViewRowCollection.AddRange(params DataGridViewRow[] dataGridViewRows)

with

new[]{someDataGridviewRow}

worked for me.



来源:https://stackoverflow.com/questions/229443/how-do-i-prevent-a-datagridview-from-sharing-rows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!