Data-Base
Data-Base

Reputation: 8618

add data to DataGrid View through DataSet?

I have a datagridview that is bound to a dataset

I defined the table and the columns, how I can programmaticlly insert data to it?

I did this http://msdn.microsoft.com/en-us/library/5ycd1034%28VS.71%29.aspx

but it did not show the data in my DataGridView !!!

Upvotes: 1

Views: 15750

Answers (3)

udit bhardwaj
udit bhardwaj

Reputation: 71

it can be bounded directly by .

DataSet ds = new DataSet();

in C#

DataGridView.DataSource = ds.Tables["your Table index which you want to bind with datagridview"];

Like

DataGridView.DataSource = ds.Tables[0];

In VB

DataGridView.DataSource = ds.Tables("your Table index which you want to bind with datagridview")

like

DataGridView.DataSource = ds.Tables(0)

Upvotes: 0

Johnny
Johnny

Reputation: 1575

Sample example

SqlDataAdapter da = new SqlDataAdapter(quryString, con);
DataSet ds = new DataSet();
da.Fill(ds, "Emp");

//dataGridView1.DataSource = ds.Tables["Emp"];
dataGridView1.Columns.Add("EmpID", "ID");
dataGridView1.Columns.Add("FirstName", "FirstName");
dataGridView1.Columns.Add("LastName", "LastName");
int row = ds.Tables["Emp"].Rows.Count - 1;

for (int r = 0; r<= row; r++)
{
dataGridView1.Rows.Add();
dataGridView1.Rows[r].Cells[0].Value = ds.Tables["Emp"].Rows[r].ItemArray[0];
dataGridView1.Rows[r].Cells[1].Value = ds.Tables["Emp"].Rows[r].ItemArray[1];
dataGridView1.Rows[r].Cells[2].Value = ds.Tables["Emp"].Rows[r].ItemArray[2];

}

Upvotes: 1

anishMarokey
anishMarokey

Reputation: 11397

now you need to assign the values to the datagridview

DataTable dt = new DataTable();

// Add rows to dt 
datagridview.DataSource = dt;

Upvotes: 5

Related Questions