Datagrid

Hi everyone,

May i know how to read all the values in a datagrid one by one. I want to add all the values in each row in a particular column in the datagrid. Is it possible to do so?

[198 byte] By [LynnOoi] at [2007-12-23]
# 1
LynnOoi wrote:

Hi everyone,

May i know how to read all the values in a datagrid one by one. I want to add all the values in each row in a particular column in the datagrid. Is it possible to do so?

Are these values that you want to read stored some where, the datagrid can be assigned to the datasource to retrieve data you want.

refer http://www.codeproject.com/cs/miscctrl/csharpgridcontrol.asp

http://p2p.wrox.com/topic.asp?TOPIC_ID=15360

hope these links would help

Sibusiso at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 2

Hi,

You would have to iterate through each row on the datagrid and get the column value for each row and keep adding them.

int numRows = datagrid1.Rows.Count;

int sum = 0;

for (int i = 0; i< numRows; i++)

{

int colValue = datagrid1[columnIndex, i].Value; //Typecase this value appropriately

sum = sum + colValue;

}

Console.WriteLine ("sum is "+sum);

RashmiGopinath at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 3

Hi,

I have try to apply those codes in my program but there is an error saying that datagrid does not contain rows property.

LynnOoi at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 4

You could get the row counts through DataTable which should be the datagrid's datasource.

int row = dt.Rows.Count;

Salalala at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 5
okok.. thanks ya!
LynnOoi at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 6

Hi,

I use dt.Rows.Count also cannot. There is an error occur. What should I do?

Here is the error message :

Compiler Error Message: CS0117: 'System.Data.SqlClient.SqlDataReader' does not contain a definition for 'Rows'

Source Error:

Line 30: DataGrid1.Visible = result;

Line 31:

Line 32: int numRows = dtrQues.Rows.Count;

Line 33: int sum = 0;

LynnOoi at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 7
dtrQues is a datareader object? I recommend you to use datatable which has the "Rows" property.
Salalala at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 8

Hi,

Is dataTable similar with repeater?

LynnOoi at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 9
Well, a datatable is not a real control like a repeater. In ADO.NET, DataTable objects are used to represent the tables in a DataSet. A DataTable represents one table of in-memory relational data; the data is local to the .NET-based application in which it resides, but can be populated from a data source such as Microsoft SQL Server using a DataAdapter
Salalala at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 10
May i know what verysion of visual studio does support datatable as i could not find datatable in the toolbox.
LynnOoi at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 11

A DataTable is an 'invisible' control that is part of ADO .NET 1.0 and above.

Suggest you either get a book on ADO.NET or Visual Studio/Basic .NET 2002 or later....not VB6 or earlier or check the MSDN library if you have access to it. Infact of the data components are in the Data folder (not the Toolbox).

Bye,

MrXtra at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 12

In addition...

A dataview can be used to search for rows using the row filter - this is definitely in the Data Folder - You may find it easier to use this....?

MrXtra at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...