Results 1 to 4 of 4

Thread: on click event

  1. #1
    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
    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?

  2. #2
    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);
    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.

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    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.

  4. #4
    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 = ********

Similar Threads

  1. How to handle JButton Click Event in Java Swing
    By Kungfu Pandey in forum Software Development
    Replies: 3
    Last Post: 20-01-2012, 12:13 PM
  2. Replies: 7
    Last Post: 22-12-2011, 05:56 AM
  3. Waiting for mouse click event
    By ISAIAH in forum Software Development
    Replies: 5
    Last Post: 05-03-2010, 11:01 AM
  4. Event Log Error: Event Source:WinMgmt Event ID:10
    By BlackSunReyes in forum Small Business Server
    Replies: 2
    Last Post: 01-03-2007, 03:27 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,251,135.36258 seconds with 16 queries