0

I have this datagridView that takes data from an object. 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?

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.

13
  • Check your ColumnIndex and RowIndex in CellClick event Commented Oct 22, 2014 at 7:27
  • What should i check? If i don't add the new column the click event works, but not with the new one @vallabha Commented Oct 22, 2014 at 7:29
  • when you click the button that you have added either exception is getting raised or the event itself is not firing. Commented Oct 22, 2014 at 7:34
  • @vallabha, yes, i get this: Index was out of range. Must be non-negative and less than the size of the collection. Commented Oct 22, 2014 at 7:35
  • then put a break point at your if condition and check ColumnIndex and RowIndex in CellClick event Commented Oct 22, 2014 at 7:37

2 Answers 2

1

you can use is operator for checking that: "is your cell a button of other"

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.

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...";
}
Sign up to request clarification or add additional context in comments.

2 Comments

thank you, one question though, how do i get the value of the first column from the row selected?
this is another question, But I answer you hear. You can use dataGridView1.Rows[dataGridView1.SelectedCells[0].RowIndex].Cells[0].Value = "Changed Me";
0

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. 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.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.