binding two tables in dataset to two gridview controls

Last post 05-17-2008 2:17 AM by nikitaprajapati. 9 replies.

Sort Posts:

  • binding two tables in dataset to two gridview controls

    03-05-2008, 10:52 AM

     Hi

    Thanks for your time.

    here is my question.I have a storedprocedure which gives 2 tables output.

    now i have those two tables in dataset. my question is

    how to assign firsttable in dataset to gridview1 and the second table to gridview2.

    i.e is there a way i can name the two tables in the dataset.

    by using the names of tables in dataset can i assign them to gridview's

    if so please give me the sample code for that.

    your help is appreciated. 

     

     

     

  • Re: binding two tables in dataset to two gridview controls

    03-05-2008, 11:11 AM
    • Loading...
    • mithileshd08
    • Joined on 02-07-2008, 9:20 PM
    • NJ, USA
    • Posts 89

    Hi

    Yes, you can do that. Here is an example.

    //dataset containing tables. 

     DataSet ds = new DataSet();

    //Binding Table 1 to 1st gridview
            GridView gv1 = new GridView();
            gv1.DataSource = ds.Tables["Table1"];
            gv1.DataBind();


    //Binding Table 2 to 2nd gridview       

    GridView gv2 = new GridView();
            gv2.DataSource = ds.Tables["Table2"];
            gv1.DataBind();

    IT will work.!! 

     

    Thanks

    Mithilesh
    --------------------------------------------------------------------------------

    Don't forget to click "Mark as Answer" on the post(s) that helped you.

  • Re: binding two tables in dataset to two gridview controls

    03-05-2008, 11:15 AM
    • Loading...
    • vinz
    • Joined on 10-05-2007, 11:47 AM
    • Cebu Philippines
    • Posts 6,471

    Yes you can.. Since you are using DataSet then you can specify the Table that you want to bind in a specific Grid.. here is an example

    SqlConnection myConnection = new SqlConnection("Your connectionString");

    SqlDataAdapter ad = new SqlDataAdapter("SPNAME", myConnection);

    DataSet ds = new DataSet();

    ad.Fill(ds);

    GridView1.DataSource = ds.Tables["Table1"];

    GridView1.DataBind();

    GridView2.DataSource = ds.Tables["Table2"];

    GridView2.DataBind();

    KeEp Rockin'
    Vincent Maverick Durano

    "Life is like music; it must be composed by ear, feeling, and instinct, not by rule..."

  • Re: binding two tables in dataset to two gridview controls

    03-05-2008, 11:26 AM

     but how can we recognize which table is binding to which datagrid

    in grid1 i want to display customer table

    in grid2 i want to display refunds table.

    can you show me how it recognizes.

     

    and one more question can i have output of stored procedure i.e two tables in a dataset.

     

    thankyou very much for your reply.

     

  • Re: binding two tables in dataset to two gridview controls

    03-05-2008, 11:42 AM

    but how can we recognize which table is binding to which datagrid

    in grid1 i want to display customer table

    in grid2 i want to display refunds table.

    can you show me how it recognizes.

     

    and one more question can i have output of stored procedure i.e two tables in a dataset.

     

    thankyou very much for your reply.

     

     

  • Re: binding two tables in dataset to two gridview controls

    03-05-2008, 1:41 PM
    • Loading...
    • vinz
    • Joined on 10-05-2007, 11:47 AM
    • Cebu Philippines
    • Posts 6,471

     If you have look at carefully my last post then you should be able answer your question.. Note that in that example

    GridView1 uses ds.Tables["Table1"]  while on the other hand GridView2 uses  ds.Tables["Table2"] which is basically coming from the Dataset

    KeEp Rockin'
    Vincent Maverick Durano

    "Life is like music; it must be composed by ear, feeling, and instinct, not by rule..."

  • Re: binding two tables in dataset to two gridview controls

    03-05-2008, 2:31 PM

    No its not assigning table1 for gridview1 . its assigning table 2 in gridview1 and nothing for gridview2.

    here is my code

    public partial class _Default : System.Web.UI.Page
    {
        SqlConnection objConnection;
        SqlCommand cmd = null;
        DataSet ds = new DataSet();
        SqlDataAdapter da;
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            GridView1.Visible = true;
            EstablishConnection();

            cmd = new SqlCommand("[dbo].[search2]", objConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            SqlParameter p1 = cmd.Parameters.Add("@val1", SqlDbType.NVarChar, 50);
            if (TextBox1.Text == "")
            {
                p1.Value = DBNull.Value;
            }
            else
            {
                p1.Value = TextBox1.Text;
            }
            SqlParameter p2 = cmd.Parameters.Add("@val2", SqlDbType.NVarChar, 50);
            if (TextBox2.Text == "")
            {
                p2.Value = DBNull.Value;
            }
            else
            {
                p2.Value = TextBox2.Text;

            }
            SqlParameter p3 = cmd.Parameters.Add("@val3", SqlDbType.NVarChar, 50);
            if (TextBox3.Text == "")
            {
                p3.Value = DBNull.Value;
            }
            else
            {
                p3.Value = TextBox3.Text;

            }
            SqlParameter p4 = cmd.Parameters.Add("@val4", SqlDbType.VarChar, 50);
            if (TextBox4.Text == "")
            {

                p4.Value = DBNull.Value;
            }
            else
            {
                p4.Value = TextBox4.Text;


            }
            //SqlParameter p5 = cmd.Parameters.Add("@val5", SqlDbType.VarChar, 50);
            da.Fill(ds);
            GridView1.DataSource = ds.Tables["Table1"];
            GridView1.DataBind();
            //GridViewRow row = GridView1.SelectedRow;
            //TableCell CNo = row.Cells[1];
            //String c = CNo.Text;
            //SqlParameter p5 = cmd.Parameters.Add("@val5", SqlDbType.VarChar, 50);
            //da.Fill(ds);
            //GridView1.DataSource = ds.Tables[0];
            CloseConnections();
                   
        }
        private void EstablishConnection()
        {
            // 1. Instantiate the Connection
            objConnection = new SqlConnection("Data Source=NAVYAACHANTA;Initial Catalog=employee;Integrated Security=SSPI");
            objConnection.Open();
        }



        private void BindDataSet(SqlDataAdapter ds)
        {
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
        private void CloseConnections()
        {

            objConnection.Close();


        }
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridView2.Visible = true;
            GridView2.DataSource = ds.Tables["Table2"];   
            GridView2.DataBind();
        }
    }

    my stored procedures returns two tables which are filled in dataset.

    but i don't know why i cannot bind each table to different gridview's

    please help me urgent.

     

  • Re: binding two tables in dataset to two gridview controls

    03-05-2008, 3:01 PM
    • Loading...
    • vinz
    • Joined on 10-05-2007, 11:47 AM
    • Cebu Philippines
    • Posts 6,471

     Thats because you did not fill the DataSet in assigned in your GridView2.. Look at the codes below.. you need to fill the DataSet with Data from your DataAdapter as what you did in Binding GridView1 da.Fill(ds)

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridView2.Visible = true;
            GridView2.DataSource = ds.Tables["Table2"];   
            GridView2.DataBind();
        }
     

    KeEp Rockin'
    Vincent Maverick Durano

    "Life is like music; it must be composed by ear, feeling, and instinct, not by rule..."

  • Re: binding two tables in dataset to two gridview controls

    03-05-2008, 3:55 PM
    Answer

    Hi i got it executed

    first i have to name the tables in dataset

    da.fill(ds);
    ds.Tables[0].TableName = "customer";
    ds.Tables[1].TableName = "refunds";

    now i have to bind it

    GridView1.DataSource = ds.Tables["customer"];
            GridView1.DataBind();
     yeah i have fill the dataset again in the click event

    for that purpose i used session varaible to store the tables.

    ds = (DataSet)Session["dsSession"];
            //da.Fill(ds);
           // ds.Tables[0].TableName = "customer";
            ds.Tables[1].TableName = "refunds";
           // GridView1.DataSource = ds.Tables["customer"];
            //GridView1.DataBind();
            
            GridView2.Visible = true;
            GridView2.DataSource = ds.Tables["refunds"];
            GridView2.DataBind();

     

     

  • Re: binding two tables in dataset to two gridview controls

    05-17-2008, 2:17 AM

    This is fine with code behind. but how to do with sqldatasource , Is it possible ? because i am having  procedure which will return 3 tables. how to do with sql datasource. by default it bind to first table..

    Is There is way????

    Nikita Prajapati
Page 1 of 1 (10 items)