Datagridview control:
DataGridView displays data from SQL databases. Here my code will show how to display a specific table from a database and display it on a DataGridView. For implementing this method we have to use one dataAdapter, which will save the data from the database and this data will fill up to the dataTable or dataSet.If we take the dataSet to fill the table from database the we have to bind the ds.Tables[int Index] to the datagridview datasource. To do that In Visual Studio, select the File menu and then New Project, and select a Windows Forms application. Next, in the designer, drag the DataGridView icon into the
window. By default it will have the name as dataGridView1, if you want to use naming conventions you can change the name of the dataGridVie from the properties window.
Event : You can write this code in Form_Load or in any button that you want to call the data from database.
cn = new SqlConnection(@"Data Source=CENSYS10\SQLEXPRESS;Integrated Security=true; Initial Catalog=master");
cn.Open();
SqlCommand cmd = new SqlCommand("select * from new", cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
dataSet ds = new dataSet();
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
Here ds.Tables[0] means the first table from the dataset. We can store more than one table in the
dataset. If you want to use dataTable directly, you can write the code as bellow
cn = new SqlConnection(@"Data Source=CENSYS10\SQLEXPRESS;Integrated Security=true; Initial Catalog=master");
cn.Open();
SqlCommand cmd = new SqlCommand("select * from new", cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
dataTable dt = new dataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
I hope this code will help you.
DataGridView displays data from SQL databases. Here my code will show how to display a specific table from a database and display it on a DataGridView. For implementing this method we have to use one dataAdapter, which will save the data from the database and this data will fill up to the dataTable or dataSet.If we take the dataSet to fill the table from database the we have to bind the ds.Tables[int Index] to the datagridview datasource. To do that In Visual Studio, select the File menu and then New Project, and select a Windows Forms application. Next, in the designer, drag the DataGridView icon into the
window. By default it will have the name as dataGridView1, if you want to use naming conventions you can change the name of the dataGridVie from the properties window.
Event : You can write this code in Form_Load or in any button that you want to call the data from database.
cn = new SqlConnection(@"Data Source=CENSYS10\SQLEXPRESS;Integrated Security=true; Initial Catalog=master");
cn.Open();
SqlCommand cmd = new SqlCommand("select * from new", cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
dataSet ds = new dataSet();
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
Here ds.Tables[0] means the first table from the dataset. We can store more than one table in the
dataset. If you want to use dataTable directly, you can write the code as bellow
cn = new SqlConnection(@"Data Source=CENSYS10\SQLEXPRESS;Integrated Security=true; Initial Catalog=master");
cn.Open();
SqlCommand cmd = new SqlCommand("select * from new", cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
dataTable dt = new dataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
I hope this code will help you.
No comments:
Post a Comment