I have a datagridview with 2 columns. Like below one
| Heading1 | Heading 2 |
| BMW | 2014 |
| Jaguar | 2005 |
| Honda | 2012 |
I need to get cell values (2014,2005,2012) of "Heading 2" column in to array.Have tried this way but not really success
foreach (DataGridViewRow row in dataGridView1.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
if (cell.ColumnIndex == 1)
{
MessageBox.Show(cell.Value.ToString());
Application.Exit();
}
}
}
How can I achieve this?
Thanks
Munesh SharmaPosted Feb 2, 2015, 6:16 AM
for (int i = 0; i < dataGridView1.Rows.Count; i++)
colB[i] = Convert.ToString(dataGridView1.Rows[i].Cells[1].Value);
RD DesignPosted Feb 2, 2015, 5:32 AM
if (dataGridView1.Rows.Count > 0)
{
for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
{
label1.Text = label1.Text + "" + dataGridView1.Rows[i].Cells[1].Value.ToString() + ",";
}
}
Saber ShaikhPosted Feb 2, 2015, 5:04 AM
RD DesignPosted Feb 2, 2015, 5:00 AM
if (dataGridView1.Rows.Count > 0)
{
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
label1.Text = label1.Text + "" + dataGridView1.Rows[i].Cells[1].Value.ToString() + ",";
}
}
But having a small issue.When there is empty cell or null value above error occurs (An unhandled exception of type System.NullReferenceException occurred).Otherwise code is working as charm.How to skip these null values and get other values?
Saber ShaikhPosted Feb 2, 2015, 4:54 AM
String[] Getdata=new String[dataGridView1.Rows.Count];
if (dataGridView1.Rows.Count > 0)
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
Getdata[i] =dataGridView1.Rows[i].Cells[1].Value.ToString() ;
}
}
100% Correct Check This
Khargesh RajputPosted Feb 2, 2015, 4:48 AM
{
for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
{
label1.Text = label1.Text+""+dataGridView1.Rows[i].Cells[1].Value.ToString() + ",";
}
}
Munesh SharmaPosted Feb 2, 2015, 4:46 AM
String[] values = new String[this.GridView1.Rows.Count];
for (int index = 0; index < this.GridView1.Rows.Count; index++)
values[index] = this.GridView1.Rows[index].Cells[0].Text;
suppose based on column name if you want to retrieve the respective cell values then iterate over grid headerrow cells, identify the cell index then use the cellindex to read the values from the grid
you change cell value according to u
Saber ShaikhPosted Feb 2, 2015, 4:42 AM
2 problem first check in cell code type your coloum no [0] or [1]
RD DesignPosted Feb 2, 2015, 4:38 AM
It was giving me "An unhandled exception of type 'System.NullReferenceException' occurred" error
in this line
label1.Text = label1.Text + "" + dataGridView1.Rows[i].Cells[1].Value.ToString() + ",";
Thanks
Saber ShaikhPosted Feb 2, 2015, 4:31 AM
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
label1.Text = label1.Text+""+dataGridView1.Rows[i].Cells[1].Value.ToString() + ",";
}
}
check this code 100% work
if is use full for u the select to Correct Answer
Blog: http://aspnettutorialscode.blogspot.in/