Important Facts about datagridview





5.00/5 (1 vote)
Here are some important facts about datagridview
Introduction
We use datagridview
to display information in a customizable grid. This is about how to populate datagridview
with data and how to get data to a data table from datagridview
and some important facts about datagridview
.
Database to DataGridView
First, create a new Windows Forms Application. Then, go to ToolBox data section and drag DataGridView
control on to the Windows Form. Then let's see how to populate datagridview
from database.
To do that, first create a SqlConnection
object and then write a SQL command to get data from the database.
try
{
//Creating SqlConnection object
SqlConnection con = new SqlConnection();
//setting the connection string
con.ConnectionString = @"Data Source=|DataDirectory|\data.sdf";
//open the connection
con.Open();
}
catch (Exception a)
{
MessageBox.Show("Database error " + a);
}
//Writing the sql command to get information from database
string command = "select * from person";
Now, create a SqlDataAdapter
object and DataTable
object and fill the DataTable
from the SqlDataAdapter
object. Now assign the DataTable
as Datagridview DataSource
.
DataTable dt = new DataTable(); //create DataTable object
try
{
//Create the SqlDataAdapter object
SqlDataAdapter adapter = new SqlDataAdapter(command , con);
//fill the Datatable from data using SqlDataAdapter
adapter.Fill(dt);
//sets the datatable as the datasource
dataGridView1.DataSource=dt;
}
catch (Exception s)
{
MessageBox.Show(s.ToString());
}
DataGridView to DataTable
Now let's see how to get information from a datagridview
to a data table. If the datagridview
has a data source, then it is too easy. Only thing you have to do is cast the datagridview
data source to a data table.
DataTable dt=(DataTable)dataGridView1.DataSource;
But in some situations, we want user to fill the datagridview
with information and later, we want to get the information to a data table. In such situations, datagridview
has no data source. Then, we have to write some code to get information from datagridview
to a data table.
DataTable datatable = new DataTable();//create DataTable object
datatable.Columns.Add("Id",typeof(int)); //add Columns to DataTable
datatable.Columns.Add("Name", typeof(string));
//read every row in datagridview
foreach (DataGridViewRow r in dataGridView1.Rows)
{
DataRow drow = datatable.NewRow(); //create a new row
//add values from datagridview row to datatable row
drow[0] = r.Cells[0].Value;
drow[1] = r.Cells[1].Value;
datatable.Rows.Add(drow); // add row to data table
}
Normally, when we enter data in a datagridview
, it will add an additional row below. So we get an additional row with no data with the relevant information. So in the previous example, you will probably get a run time error. To avoid that, you can check for null
rows.
try
{
if (r.Cells[0].Value == null || r.Cells[1].Value == null)
throw new ArgumentNullException("Values cannot be null");
else
{
drow[0] = r.Cells[0].Value;
drow[1] = r.Cells[1].Value;
dt1.Rows.Add(drow);
}
}
catch (ArgumentNullException err)
{
Console.WriteLine(err.Message);
}
Now let's see some important properties of datagridview
.
When we add columns to datagridview
normally columns won’t fit to entire datagridview
space. We can adjust that using:
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
We can change the datagridview
selection mode using:
//select full row
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
//select full column
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullColumnSelect;
Read more @ http://msdn.microsoft.com/en-us/library/wc5cbb9z.