-
on click event
in a dataviewgrid on a click event on main form lets call the button show payroll---
when someone selects an employee id on main form and hits the show payroll button a new form opens up.
the 2nd form is supposed to show an unbound datagridview displaying only that selected employees payroll information.
2 tables
employeestbl
payrolltbl
how do i populate the datagridview on the 2nd form when it is unbound? remember there are 2 forms ---
and where do i put said code? under which form and under which event?
-
Re: on click event
Let us put GridView1 control on the web form from Toolbox. The coding is straight forward and is like the following:
protected void Page_Load(object sender, EventArgs e)
{
string strSQLconnection = "Data Source=dbServer;Initial Catalog=testDB;Integrated Security=True";
SqlConnection sqlConnection = new SqlConnection(strSQLconnection);
SqlCommand sqlCommand = new SqlCommand("select * from table1", sqlConnection);
sqlConnection.Open();
SqlDataReader reader = sqlCommand.ExecuteReader();
GridView1.DataSource = reader;
GridView1.DataBind();
}
You run the code and you can see the result. But when you see the data binding for DataGridView in the following section, it is quite different.
-
Re: on click event
When I used the DataGridView control in C# in MS Visual Studio 2005, I found DataGridView control is not friendly to use. Windows form-based DataGridView control is different from web based GridView control. DataGridView doesn’t have DataBind() method. It took me a few days to figure out.
The logic is like this
Create data set from SQL statement or stored procedure
Create a table to hold this data set
Create a BindingSource and bind this table with this BindingSource
Then bind this BindingSource with GridView control.
This looks trivial. But I found it is very efficient and error free.
Let us put DataGridView control and BindingSource control on the windows form from Toolbox. Let us name DataGridView control as dbGridView, BindingSource control as dbBindSource. Let us apply the following code:
private void Form1_Load(object sender, EventArgs e)
{
string strCon = "Data Source=dbServer;Initial Catalog=testDB;Integrated Security=True";
string strSQL = “select * from table1”;
SqlDataAdapter dataAdapter = new SqlDataAdapter(strSQL, strCon);
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
// Populate a new data table and bind it to the BindingSource.
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
dbBindSource.DataSource = table;
// Resize the DataGridView columns to fit the newly loaded content.
dbGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
// you can make it grid readonly.
dbGridView.ReadOnly = true;
// finally bind the data to the grid
dbGridView.DataSource = dbBindSource;
}
Now we compile it and run it. You can see the data in the grid.
-
Re: on click event
thanx i was missing 1 line of code in my VB statement...
The line of temp = ********
Page generated in 1,713,378,332.91312 seconds with 10 queries