Saturday, September 4, 2010

Datagrid (c# Windows Form) : How to get the value of a column which is in the same row of the check box cell that user has clicked ?

The solution is here :

static int SelectColumnIndex = 0;
PerformAction_Click(object sender, System.EventArgs e)
{
string data = string.Empty;
foreach(DataGridViewRow row in MyDataGridView.Rows)
{
if(row.Cells[SelectColumnIndex].Value!=null &&
Convert.ToBoolean(row.Cells[SelectColumnIndex].Value) == true)
{
foreach(DataGridViewCell cell in row.Cells)
{
if(cell.OwningColumn.Index!= SelectColumnIndex)
{
data
+= (cell.Value + " ") ; // do some thing
}
}
data
+="\n";
}
}
MessageBox.Show(data, "Data");
}

And if we want to get the value of a particular column, use the code below :

static int SelectColumnIndex = 0;
PerformAction_Click(object sender, System.EventArgs e)
{
string data = string.Empty;
foreach(DataGridViewRow row in MyDataGridView.Rows)
{
if(row.Cells[SelectColumnIndex].Value!=null &&
Convert.ToBoolean(row.Cells[SelectColumnIndex].Value) == true)
{
foreach(DataGridViewCell cell in row.Cells)
{
if(cell.OwningColumn.Index!= SelectColumnIndex)
{
data
+= (row.Cells[1].Value + " ") ; // do some thing
}
}
data
+="\n";
}
}
MessageBox.Show(data, "Data");
}

1 comment:

  1. Hoping to get success Row.Dock Style property by the help of the article.Its also useful in data grid.
    http://www.dapfor.com/en/net-suite/net-grid/features/headers-and-columns

    ReplyDelete