Data Platform Developer Center >
Data Platform Development Forums
>
ADO.NET DataSet
>
using GridView how to do
using GridView how to do
when i give input as accountno then i press display button ,it should fetch and display in gridview...how to write in C# using ado.net ??
Answers
- Many more ways than one to do this depending on your setup, requierments, etc. But a simple example:
private void button1_Click(object sender, EventArgs e) { using (SqlConnection con = new SqlConnection(cs)) { con.Open(); SqlCommand cmd = new SqlCommand("select * from Employees where EmployeeID = @empId", con); cmd.Parameters.AddWithValue("@empId", 2); SqlDataAdapter adp = new SqlDataAdapter(cmd); adp.Fill(ds, "Employees"); con.Close(); } dataGridView1.DataSource = ds; dataGridView1.DataMember = "Employees"; }
HTH
//Michael
This posting is provided "AS IS" with no warranties.- Marked As Answer byVMazurMVP, ModeratorWednesday, November 04, 2009 11:21 AM
All Replies
- Many more ways than one to do this depending on your setup, requierments, etc. But a simple example:
private void button1_Click(object sender, EventArgs e) { using (SqlConnection con = new SqlConnection(cs)) { con.Open(); SqlCommand cmd = new SqlCommand("select * from Employees where EmployeeID = @empId", con); cmd.Parameters.AddWithValue("@empId", 2); SqlDataAdapter adp = new SqlDataAdapter(cmd); adp.Fill(ds, "Employees"); con.Close(); } dataGridView1.DataSource = ds; dataGridView1.DataMember = "Employees"; }
HTH
//Michael
This posting is provided "AS IS" with no warranties.- Marked As Answer byVMazurMVP, ModeratorWednesday, November 04, 2009 11:21 AM
private void button1_Click(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(cs))
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from Employees where EmployeeID = @empId" , con);
cmd.Parameters.AddWithValue("@empId" , 2);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(ds, "Employees" );
con.Close();
}
dataGridView1.DataSource = ds.Tables[0];
dataGridView1.DataMember = "Employees" ;
}
Tejas Mer- Not to be picky, but there is no problem to have a dataset as a datasource.
Then you just specify what table (DataMember) you wish to use for the time being.
May work differently in a web app, but this is how works on WinForms
DataGridView.DataSource Property
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.datasource.aspx
DataGridView.DataMember Property
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.datamember.aspx
//Michael
This posting is provided "AS IS" with no warranties.


