[英]C# DataGridView Header
Firstly I'm quite new with programming(first week). 首先,我对编程很陌生(第一周)。 I have a datagridviewer and CellClick event on it.
我上面有一个datagridviewer和CellClick事件。 What my function do is, gets the first column value of selected(mouse left clicked) cell of row.
我的功能是获取行的选定(鼠标左键单击)单元格的第一列值。 Like;
喜欢;
ID Name Address ( this ones Headers of gridview) ID名称地址(这是gridview的标题)
1 User1 Address1 1个用户1地址1
2 User2 Address2 2用户2地址2
..6,7.. ..6,7 ..
9 User9 Address9 9用户9地址9
When you click on any cell -except Headers- it gives the value of first column. 当您单击任何单元格(标题除外)时,它将给出第一列的值。 When you click, Address9 your value will return as 9 etc.
当您单击Address9时,您的值将返回为9等。
I get outofexception error when header cell is clicked. 单击标题单元格时出现异常错误。 Which returns -1 value and crashs.So, I need a if statement to ignore header clicks.
它返回-1值并崩溃。因此,我需要一个if语句来忽略标题点击。 I searched it on websites and tried many things myself but haven't managed to achieve this.
我在网站上进行了搜索,然后自己尝试了许多方法,但未能实现这一目标。
private void dgwCust_CellClick(object sender, DataGridViewCellEventArgs e)
{
if ( cellclick == isn't a header click?.. )
string search;
search = dgwCust.Rows[e.RowIndex].Cells[0].Value.ToString();//this is where error comes in
SqlCommand cmdsearch = new SqlCommand("Select * from Customers where CustomerID = '" + search + "'", dbConnection);
try
{
etc.. 等等..
Check if e.RowIndex does not equal -1 by using one of the following 通过使用以下方法之一检查e.RowIndex不等于-1
if (!e.RowIndex.Equals(-1))
or 要么
if (e.RowIndex != -1)
- -
private void dgwCust_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (!e.RowIndex.Equals(-1))
string search;
search = dgwCust.Rows[e.RowIndex].Cells[0].Value.ToString();//this is where error comes in
SqlCommand cmdsearch = new SqlCommand("Select * from Customers where CustomerID = '" + search + "'", dbConnection);
try
{
if (e.RowIndex >= 0)
{
string search;
search = dgwCust.Rows[e.RowIndex].Cells[0].Value.ToString();//this is where error comes in
SqlCommand cmdsearch = new SqlCommand("Select * from Customers where CustomerID = '" + search + "'", dbConnection);
//...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.