Rows are not insert in windows form datagrid

Posted by M3nhaq under C# on 11/22/2011 | Points: 10 | Views : 1366 | Status : [Member] | Replies : 1
Hi.
I want to insert data grid using datatable and define the grid columns in a program. Please help me what`s wrong with this code???

SqlConnection connection;
string query;
SqlCommand cmd;
SqlDataAdapter da;
DataSet ds;
DataRow dRows;

public void ViewGrid()
{
connection = new SqlConnection(ConnString);
try
{
connection.Open();
query = @"SELECT * FROM TBLWorkers";
//cmd = new SqlCommand(query, connection);
da = new SqlDataAdapter(query, connection);
ds = new DataSet();
da.Fill(ds, "Workers");

int MaxRows = ds.Tables["Workers"].Rows.Count;

label1.Text = MaxRows.ToString();

dRows = ds.Tables["Workers"].Rows[0];


// Create an unbound DataGridView by declaring a column count.
dataGridView1.ColumnCount = 4;
dataGridView1.ColumnHeadersVisible = true;

// Set the column header names.
dataGridView1.Columns[0].Name = "Recipe";
dataGridView1.Columns[1].Name = "Category";
dataGridView1.Columns[2].Name = "Main Ingredients";
dataGridView1.Columns[3].Name = "Rating";

object[] rows1 = new object[] { dRows[0].ToString(), dRows[1], dRows[2], dRows[3] };
foreach (string[] rowArray in rows1)
{
dataGridView1.Rows.Add(rowArray);
}

}
catch (Exception x)
{
MessageBox.Show(x.Message);
connection.Close();
}

}





Responses

Posted by: Naraayanan on: 1/6/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,
Could you get any error?If Yes ,Please send your error.
Just Fill the Data in the Datagridview..
dataGridView1.DataSource = ds.Tables[0];



Regards,
Lakshmi Naraayanan.S
http://dotnettechrocks.blogspot.in/
http://abaprocker.blogspot.com/

M3nhaq, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response