datagridview取消默认选中_.net c#在WinForm应用程序中加载表单后,禁用datagridview中的默认单元格选择...

I have a dataGridView control in Window Form Application. Three columns are added to the dataGridView. I added 20 empty rows into dataGridView in order to show some blank cells.(Otherwise dataGridView just shows grey area).

On dataGridView control, I add three column "a","b", "c". In form load, I add this,

dataGridView1.Rows.Add(20);

after form initialize, datagridview shows some blank cell, but first cell in first row is selected(highlighted with blue color). Please note, there is no data in datagridview. I could not figure out how to clear the default selection.

I did some research. There are some discussion. I tried, ,and they were not working. I think it is because I don't have data in the control. But the method mentioned in the following link, is for a datagridview loaded with data.

Also there are some discussion regarding Windows store application. (I am using windows form application)

Thanks in advance if you can give me some ideas.

Talk1:

No. Setting the Selected property of the said cell to false works just fine without any data. If you don't succeed (and I remember having had weird problems with that last year) do this: Set it to true for another cell and then back to false. It will work, promise!

Talk2:

: Add this in form load : dataGridView3.Rows[0].Cells[0].Selected = false; Not working. Then try this, dataGridView3.Rows[0].Cells[1].Selected = true; dataGridView3.Rows[0].Cells[1].Selected = false; Still no working. Am I wrong somewhere?

Talk3:

Hm, I don't know. I think I set MultiSelect to false before and reset it afterwards..

Talk4:

: BTW, even I try dataGridView3.Rows[0].Cells[0].Selected = false; ataGridView3.Rows[0].Cells[2].Selected = true; Still the first cell in first row is selected...

Talk5:

: OK, it is working after I set MultiSelect to false.

Solutions1

I have used this code in my project's form's Shown-event:

dataGridView1.ClearSelection();

Talk1:

u r a lifesaver man!!!

Solutions2

DataGridView1.FirstDisplayedCell.Selected = False

Solutions3

I can't reproduce in a fresh test DGV but I have seen it before.

Here is what I found to work:

bool old = dataGridView1.MultiSelect;

dataGridView1.MultiSelect = false;

dataGridView1.Rows[1].Cells[0].Selected = true;

dataGridView1.Rows[1].Cells[0].Selected = false;

dataGridView1.MultiSelect = old;

I don't know what causes this sometimes..

(..but I will add it to the answer once I do.)

Solutions4

In my case, i got it working using this piece of code:

dataGridView1.EndEdit();

Hope this helps someone.

Solutions5

If anyone is still having troubles with this I have found this worked for me when all the above suggestions failed.

On the DataGridView create an event for DataBindingComplete then add this method datagridview1.ClearSelection();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值