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.
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);
SqlDataReader reader = sqlCommand.ExecuteReader();
GridView1.DataSource = reader;
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;
dbBindSource.DataSource = table;
// Resize the DataGridView columns to fit the newly loaded content.
// 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.
|Tags: click event|
|Thread Tools||Search this Thread|
|Similar Threads for: "on click event"|
|Thread||Thread Starter||Forum||Replies||Last Post|
|How to handle JButton Click Event in Java Swing||Kungfu Pandey||Software Development||3||20-01-2012 12:13 PM|
|Mighty Mouse left and right click works as a middle click on MacBook pro||Shrestha||Hardware Peripherals||7||22-12-2011 05:56 AM|
|Waiting for mouse click event||ISAIAH||Software Development||5||05-03-2010 11:01 AM|
|Event Log Error: Event Source:WinMgmt Event ID:10||BlackSunReyes||Small Business Server||2||01-03-2007 03:27 AM|