Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Thread Tools Search this Thread
Old 04-03-2009
Join Date: Feb 2009
Posts: 96
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

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?
Reply With Quote
Old 04-03-2009
Join Date: Apr 2008
Posts: 1,948
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.
Reply With Quote
Old 04-03-2009
Join Date: May 2008
Posts: 2,008
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.
Reply With Quote
Old 06-03-2009
Join Date: Feb 2009
Posts: 96
Re: on click event

thanx i was missing 1 line of code in my VB statement...

The line of temp = ********
Reply With Quote

  TechArena Community > Software > Software Development

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

All times are GMT +5.5. The time now is 05:04 PM.