How to change the button's value in Datagrid
-
Hi Everyone, I am using button's obj in datagridview and set the flag as true to show the text value of the button (Name of the Button). If user clicks on the button, its changes the button’s name based upon the criteria. I am doing very simple code here. I set button’s name as “buttonColumn.UseColumnTextForButtonValue = true;” Now If I want to change the value with following settings. It did not work it out. dataGridView1[e.ColumnIndex, e.RowIndex].Value = "America"; It is still taking "Name" Can you please help me on this? private void CreateButtonColumn() { // Initialize the button column. DataGridViewButtonColumn buttonColumn = new DataGridViewButtonColumn(); // Add the button column to the control. buttonColumn = new DataGridViewButtonColumn(); buttonColumn.Name = "Name"; buttonColumn.Text = "Name"; buttonColumn.Width = 110; buttonColumn.UseColumnTextForButtonValue = true; dataGridView1.Columns.Insert(0, buttonColumn); dataGridView1.Rows.Add(2); dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick); } void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { if (Convert.ToInt32(e.ColumnIndex) == 0) { dataGridView1[e.ColumnIndex, e.RowIndex].Value = "America"; } } Thanks.
-
Hi Everyone, I am using button's obj in datagridview and set the flag as true to show the text value of the button (Name of the Button). If user clicks on the button, its changes the button’s name based upon the criteria. I am doing very simple code here. I set button’s name as “buttonColumn.UseColumnTextForButtonValue = true;” Now If I want to change the value with following settings. It did not work it out. dataGridView1[e.ColumnIndex, e.RowIndex].Value = "America"; It is still taking "Name" Can you please help me on this? private void CreateButtonColumn() { // Initialize the button column. DataGridViewButtonColumn buttonColumn = new DataGridViewButtonColumn(); // Add the button column to the control. buttonColumn = new DataGridViewButtonColumn(); buttonColumn.Name = "Name"; buttonColumn.Text = "Name"; buttonColumn.Width = 110; buttonColumn.UseColumnTextForButtonValue = true; dataGridView1.Columns.Insert(0, buttonColumn); dataGridView1.Rows.Add(2); dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick); } void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { if (Convert.ToInt32(e.ColumnIndex) == 0) { dataGridView1[e.ColumnIndex, e.RowIndex].Value = "America"; } } Thanks.
I've not tried this but from your code, an obvious thing to try would seem to be
void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (Convert.ToInt32(e.ColumnIndex) == 0)
{
((DataGridViewButtonColumn)dataGridView1.Columns[e.ColumnIndex]).UseColumnTextForButtonValue = false;
dataGridView1[e.ColumnIndex, e.RowIndex].Value = "America";
}
}Hope this helps. :)
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
I've not tried this but from your code, an obvious thing to try would seem to be
void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (Convert.ToInt32(e.ColumnIndex) == 0)
{
((DataGridViewButtonColumn)dataGridView1.Columns[e.ColumnIndex]).UseColumnTextForButtonValue = false;
dataGridView1[e.ColumnIndex, e.RowIndex].Value = "America";
}
}Hope this helps. :)
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
Thanks. This is very good one. Just to mention one thing here. Its changes all the rows with new value "America". I want to updates the same cell where was cliked. Once Again Thanks
Ah! :-O Well, it was worth a try. I have searched through my own code for examples of using a
DataGridviewButtonColumn
and all of the examples I found used the button click to load a data entry form to get the required data. on return from the data entry form they used the following syntax to set the value(s) entered:void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (Convert.ToInt32(e.ColumnIndex) == 0)
{
dataGridView1.Rows[e.RowIndex].Cells["Name"].Value = "America";
}
}which is setting the value of the appropriate
Cell
in the underlyingDataGridViewRow
.Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”