简体   繁体   English

datagridView中的添加按钮不起作用onClick事件

[英]Adding button in datagridView not working onClick event

I have this datagridView that takes data from an object. 我有这个datagridView从对象中获取数据。 I add columns like this: 我添加这样的列:

dataGridView1.CellClick += dataGridView1_CellClick;
DataGridViewButtonColumn colUsers = new DataGridViewButtonColumn();
colUsers.UseColumnTextForButtonValue = true;
colUsers.Text = "Users";
colUsers.Name = "";
dataGridView1.Columns.Add(colUsers);

And I add an onclick event, but it's not working, am I missing something? 而且我添加了一个onclick事件,但是它不起作用,我丢失了什么吗?

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{

    if (e.RowIndex > -1 && dataGridView1.Columns[e.ColumnIndex].Name == "Users")
    {
        name = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
        gtUserDetails.ShowDialog();
    } 
}

I get an error: Index was out of range. 我收到一个错误: 索引超出范围。 Must be non-negative and less than the size of the collection. 必须为非负数并且小于集合的大小。

you can use is operator for checking that: "is your cell a button of other" 您可以使用is运算符检查以下内容: “您的单元格是否是其他按钮”

and use CellContentClick instead CellClick , because if user click on padding of your button, your event don't raise and wait for clicking ON your button. 并使用CellContentClick而不是CellClick ,因为如果用户单击按钮的填充,则事件不会CellClick并等待单击按钮的ON。

Therefor, you can use this event 因此,您可以使用此事件

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    if (dataGridView1[e.ColumnIndex,e.RowIndex] is DataGridViewButtonCell)
        (dataGridView1[e.ColumnIndex, e.RowIndex] as DataGridViewButtonCell).Value = "You Clicked Me...";
}

Perhaps this is a flaw BUT: 也许这是一个缺陷,但:

colUsers.Name = ""; 

sets your columnname on an empty string instead of "Users". 将列名设置为空字符串,而不是“用户”。 the property Text isn't the same as property Name. 属性文本与属性名称不同。

colUsers.Name = "Users";

EDIT: Constant strings 编辑:常量字符串

Whenever you want to use string values inside your code, plz start using a Constant reference. 每当您想在代码中使用字符串值时,plz就会开始使用常量引用。 This will keep your string values in 1 place instead of reusing them all the time where the possibility lays that you give in the wrong info, resulting in wrong results. 这样会将您的字符串值保留在1个位置,而不是在有可能输入错误信息并导致错误结果的情况下始终重复使用它们。

for example 例如

const readonly string UserbuttonName = "Users";

private void CreatebuttonName()
{
  colUsers.Name = UserbuttonName;
}

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
 if (e.RowIndex > -1 && dataGridView1.Columns[e.ColumnIndex].Name == UserbuttonName)
  DoSomething();
}

EDIT: a complete list of properties 编辑:属性的完整列表

Datagridviewbutton column properties : http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewbuttoncolumn_properties(v=vs.110).aspx Datagridviewbutton列属性: http : //msdn.microsoft.com/zh-cn/library/system.windows.forms.datagridviewbuttoncolumn_properties( v=vs.110) .aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM