Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > How to get value of all rows and column from datagrid
 

How to get value of all rows and column from datagrid

I am using datagrid to display salary slip of particular employee iand deply that slip to emplyee outlook inbox folder but
there is a problem i cant get all rows value and column value in mailitem.body
May u help me through code

thanks
Avnindra kumar
Avnindra kumar  Friday, July 17, 2009 6:48 AM

Hi Avnindra,

Do you mean that you want to traverse all the values in the datagrid? Which datagrid are you using? DataGrid or DataGridView?

If so, this is the code snippet:
//Traverse DataGrid the data source of which is a DataTable

DataTable dt = dataGrid1.DataSource as DataTable;

for (int row = 0; row < dt.Rows.Count; row++)

for (int col = 0; col < dt.Columns.Count; col++)

{

//Get the value of the cell

object val = dataGrid1[row, col];

}

//Traverse DataGridView

for (int row = 0; row < dataGridView1.Rows.Count; row++)

for (int col = 0; col < dataGridView1.Columns.Count; col++)

{

//Get the value of the cell

object val = dataGridView1[col, row].Value;

}

Please feel free to tell me if I misunderstand you.

Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Monday, July 20, 2009 5:10 AM

Hi Avnindra,

Do you mean that you want to traverse all the values in the datagrid? Which datagrid are you using? DataGrid or DataGridView?

If so, this is the code snippet:
//Traverse DataGrid the data source of which is a DataTable

DataTable dt = dataGrid1.DataSource as DataTable;

for (int row = 0; row < dt.Rows.Count; row++)

for (int col = 0; col < dt.Columns.Count; col++)

{

//Get the value of the cell

object val = dataGrid1[row, col];

}

//Traverse DataGridView

for (int row = 0; row < dataGridView1.Rows.Count; row++)

for (int col = 0; col < dataGridView1.Columns.Count; col++)

{

//Get the value of the cell

object val = dataGridView1[col, row].Value;

}

Please feel free to tell me if I misunderstand you.

Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Monday, July 20, 2009 5:10 AM

You can use google to search for other answers

Custom Search

More Threads

• implement a combo box column and add new columns to existing table
• strange behavior with DataGridViewImageColumn
• List Box and Data Grid
• Data bound items on a form automation and DataSet generation
• "Like" Clause is Not Working Using VS and Access db
• Formatting databound textbox
• user crontrols
• Datagridview Row's DefaultCellStyle
• Why I can not set any DefaultCellStyle after I set DataSource
• Filling a drop down combo box on the toolstrip