Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How to assign 2 dataset to gridview in asp.net using C#.

Posted on 2011-10-11
17
Medium Priority
?
410 Views
Last Modified: 2012-05-12
I have a gridview, i want to assign two dataset to it.
1. first one should populate the data in gridview.
2. There is drop down in 5th column, and dsstates should be populated.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table border="1">
    <tr>
    <td>
    <div align="right">
        <asp:Label ID="Label1" runat="server" Text="Select State"></asp:Label>
    </div>
    </td>
    <td>
    <div align="right">
        <asp:DropDownList ID="ddlstates" runat="server" Width="200px"
            onselectedindexchanged="ddlstates_SelectedIndexChanged"
            AutoPostBack="True">
        </asp:DropDownList>
    </div>
    </td>
    </tr>
    <tr>
    <td>
    <div align="right">
        <asp:Label ID="Label2" runat="server" Text="Select District"></asp:Label>
    </div>
    </td>
    <td>
    <div align="right">
        <asp:DropDownList ID="ddldistricts" runat="server" Width="200px"
            onselectedindexchanged="ddldistricts_SelectedIndexChanged" AutoPostBack="True">
        </asp:DropDownList>
    </div>
    </td>
    </tr>
   
    </table>
    <asp:GridView ID="g1" runat="server" AutoGenerateColumns="false" >
           <Columns>
                <asp:TemplateField HeaderText="States">                            
                    <ItemTemplate>
                        <asp:DropDownList ID="ddlstates" runat="server"  DataTextField ="state" DataValueField ="state" DataSourceID ="dsstate" >
                         
                        </asp:DropDownList>                                
                    </ItemTemplate>
                </asp:TemplateField>
               
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>


public partial class _Default : System.Web.UI.Page
{
    SqlDataAdapter da;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindstates();
        }
    }
    public void bindstates()
    {
        da = new SqlDataAdapter("Select distinct state from r where persontype like '%TON%' order by state", ConfigurationManager.ConnectionStrings["sSqlConn"].ConnectionString);
        DataSet ds = new DataSet();
        da.Fill(ds);
        ddlstates.DataSource = ds;
        ddlstates.DataTextField = "state";
        ddlstates.DataValueField = "state";

        ddlstates.DataBind();
        ListItem li = new ListItem();
        li.Text = "--Select State--";
        li.Value = "0";
        ddlstates.Items.Insert(0, li);
    }

    protected void ddlstates_SelectedIndexChanged(object sender, EventArgs e)
    {

        ddldistricts.SelectedIndex = -1;
        if (ddlstates.SelectedItem.Text != "--Select State--")
        {
            binddistricts();
        }

    }
    public void binddistricts()
    {
        da = new SqlDataAdapter("Select distinct district from r where persontype like '%TON%' and state='" + ddlstates.SelectedItem.Text.Trim() + "' order by district", ConfigurationManager.ConnectionStrings["sSqlConn"].ConnectionString);
        DataSet ds = new DataSet();
        da.Fill(ds);
        DataSet dsstate = new DataSet();
        da.Fill(dsstate);
       
        ddldistricts.DataSource = ds;
        ddldistricts.DataTextField = "district";
        ddldistricts.DataValueField = "district";

        ddldistricts.DataBind();
        ListItem li = new ListItem();
        li.Text = "--Select District--";
        li.Value = "0";
        ddldistricts.Items.Insert(0, li);
    }


    protected void ddldistricts_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddldistricts.SelectedItem.Text != "--Select District--")
        {
            binddata();
        }

    }
    public void binddata()
    {
        da = new SqlDataAdapter("Select state,district from statedistrict order by 1,2",ConfigurationManager.ConnectionStrings["sSqlConn"].ConnectionString);
        DataSet ds = new DataSet();
        da.Fill(ds);
        if (ds.Tables[0].Rows.Count != 0)
        {
            g1.DataSource = ds;
            g1.DataBind();
        }
    }
}

0
Comment
Question by:searchsanjaysharma
17 Comments
 
LVL 8

Expert Comment

by:guvera
ID: 36953839
Hi,
 
   Please check the below link.
 
   http://aspalliance.com/148_Merging_two_Datasets_into_a_single_Datagrid

 Regards
 Guvera
0
 

Author Comment

by:searchsanjaysharma
ID: 36953844
soory no use
0
 
LVL 6

Expert Comment

by:kswathi
ID: 36954084
for the drop down in 5th column, and dsstates should be populated.
You need to write an  row  data bound event for the gridview  in that event find the control and bind the states.
check the below link to bind the dropdown inside a grid view.
http://www.aspnettutorials.com/tutorials/controls/dropdownlist-gridview-csharp.aspx
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:Ramkisan Jagtap
ID: 36954428
Hi sanjay,
You can bind that Dropdownlist on RowDataBound Event of the grid view like below. Just remove the

<asp:GridView ID="g1" runat="server" AutoGenerateColumns="false" >
           <Columns>
                <asp:TemplateField HeaderText="States">                            
                    <ItemTemplate>
                      <asp:DropDownList ID="ddlstates" runat="server"  DataTextField ="state" DataValueField ="state" >
                       </asp:DropDownList>                                
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

 void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
  {

    if(e.Row.RowType == DataControlRowType.DataRow)
    {
            DropDownList ddlStates=(DropDownList )e.Row.FindControl("ddlstates");
            da = new SqlDataAdapter("Select distinct state from r where persontype like '%TON%' order by state",  
                    ConfigurationManager.ConnectionStrings["sSqlConn"].ConnectionString);
        DataSet ds = new DataSet();
        da.Fill(ds);
        ddlStates.DataSource = ds;
        ddlStates.DataTextField = "state";
        ddlStates.DataValueField = "state";

        ddlStates.DataBind();
        ListItem li = new ListItem();
        li.Text = "--Select State--";
        li.Value = "0";
        ddlstates.Items.Insert(0, li);
    }

  }

0
 
LVL 12

Expert Comment

by:Ramkisan Jagtap
ID: 36954441
just create your gridview's RowDataBound and paste the code

 if(e.Row.RowType == DataControlRowType.DataRow)
    {
            DropDownList ddlStates=(DropDownList )e.Row.FindControl("ddlstates");
            da = new SqlDataAdapter("Select distinct state from r where persontype like '%TON%' order by state",  
                    ConfigurationManager.ConnectionStrings["sSqlConn"].ConnectionString);
        DataSet ds = new DataSet();
        da.Fill(ds);
        ddlStates.DataSource = ds;
        ddlStates.DataTextField = "state";
        ddlStates.DataValueField = "state";

        ddlStates.DataBind();
        ListItem li = new ListItem();
        li.Text = "--Select State--";
        li.Value = "0";
        ddlstates.Items.Insert(0, li);
    }

Thanks
0
 

Author Comment

by:searchsanjaysharma
ID: 36957878
I have done this, but not working. I want 3 things.
1. Drop down to be added in last column.
2. Gridview will have data populated from table statedistrict
3. For every row the new added dropdown should have its values from database.
The code is as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table border="1">
    <tr>
    <td>
    <div align="right">
        <asp:Label ID="Label1" runat="server" Text="Select State"></asp:Label>
    </div>
    </td>
    <td>
    <div align="right">
        <asp:DropDownList ID="ddlstates" runat="server" Width="200px"
            onselectedindexchanged="ddlstates_SelectedIndexChanged"
            AutoPostBack="True">
        </asp:DropDownList>
    </div>
    </td>
    </tr>
    <tr>
    <td>
    <div align="right">
        <asp:Label ID="Label2" runat="server" Text="Select District"></asp:Label>
    </div>
    </td>
    <td>
    <div align="right">
        <asp:DropDownList ID="ddldistricts" runat="server" Width="200px"
            onselectedindexchanged="ddldistricts_SelectedIndexChanged" AutoPostBack="True">
        </asp:DropDownList>
    </div>
    </td>
    </tr>
   
    </table>
    <asp:GridView ID="g1" runat="server" OnRowDataBound="g1_RowDataBound" >
     </asp:GridView>
    </div>
    </form>
</body>
</html>
/////////////////////////////////////////

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class _Default : System.Web.UI.Page
{
    SqlDataAdapter da;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindstates();
        }
    }
    public void bindstates()
    {
        da = new SqlDataAdapter("Select distinct state from r where persontype like '%TON%' order by state", ConfigurationManager.ConnectionStrings["sSqlConn"].ConnectionString);
        DataSet ds = new DataSet();
        da.Fill(ds);
        ddlstates.DataSource = ds;
        ddlstates.DataTextField = "state";
        ddlstates.DataValueField = "state";

        ddlstates.DataBind();
        ListItem li = new ListItem();
        li.Text = "--Select State--";
        li.Value = "0";
        ddlstates.Items.Insert(0, li);
    }

    protected void ddlstates_SelectedIndexChanged(object sender, EventArgs e)
    {

        ddldistricts.SelectedIndex = -1;
        if (ddlstates.SelectedItem.Text != "--Select State--")
        {
            binddistricts();
        }

    }
    public void binddistricts()
    {
        da = new SqlDataAdapter("Select distinct district from r where persontype like '%TON%' and state='" + ddlstates.SelectedItem.Text.Trim() + "' order by district", ConfigurationManager.ConnectionStrings["sSqlConn"].ConnectionString);
        DataSet ds = new DataSet();
        da.Fill(ds);
        DataSet dsstate = new DataSet();
        da.Fill(dsstate);
       
        ddldistricts.DataSource = ds;
        ddldistricts.DataTextField = "district";
        ddldistricts.DataValueField = "district";

        ddldistricts.DataBind();
        ListItem li = new ListItem();
        li.Text = "--Select District--";
        li.Value = "0";
        ddldistricts.Items.Insert(0, li);
    }


    protected void ddldistricts_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddldistricts.SelectedItem.Text != "--Select District--")
        {
            binddata();
        }

    }
    public void binddata()
    {
        da = new SqlDataAdapter("Select state,district from statedistrict order by 1,2",ConfigurationManager.ConnectionStrings["sSqlConn"].ConnectionString);
        DataSet ds = new DataSet();
        da.Fill(ds);
        if (ds.Tables[0].Rows.Count != 0)
        {
            g1.DataSource = ds;
            g1.DataBind();
        }
    }

    protected void g1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList ddlStates = (DropDownList)e.Row.FindControl("ddlstates");
            da = new SqlDataAdapter("Select distinct state from r where persontype like '%TON%' order by state",ConfigurationManager.ConnectionStrings["sSqlConn"].ConnectionString);
            DataSet ds = new DataSet();
            da.Fill(ds);
            ddlstates.DataSource = ds;
            ddlstates.DataTextField = "state";
            ddlstates.DataValueField = "state";

            ddlstates.DataBind();
            ListItem li = new ListItem();
            li.Text = "--Select State--";
            li.Value = "0";
            ddlstates.Items.Insert(0, li);
        }
    }
}



0
 
LVL 12

Expert Comment

by:Ramkisan Jagtap
ID: 36958003
please add first 4 field in gridview as bound field and last one as template field in gridview.
0
 

Author Comment

by:searchsanjaysharma
ID: 36958083

like this. but not working
<asp:GridView ID="g1" runat="server" OnRowDataBound="g1_RowDataBound">
    <Columns>
    <asp:boundfield datafield="state" headertext="state" />
    <asp:boundfield datafield="district" headertext="district" />
   
    <asp:templatefield headertext="state">
    <itemtemplate>
    <asp:dropdownlist id="ddlstates" runat="server">
    </asp:dropdownlist>
    </itemtemplate>
    </asp:templatefield>
             </Columns>
           

         
        </asp:GridView>
0
 

Author Comment

by:searchsanjaysharma
ID: 36958122
The gridview should be acticated on this event.
 protected void ddldistricts_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddldistricts.SelectedItem.Text != "--Select District--")
        {
            binddata();
        }

    }
    public void binddata()
    {
        da = new SqlDataAdapter("Select state,district from statedistrict order by 1,2",ConfigurationManager.ConnectionStrings["sSqlConn"].ConnectionString);
        DataSet ds = new DataSet();
        da.Fill(ds);
        if (ds.Tables[0].Rows.Count != 0)
        {
            g1.DataSource = ds;
            g1.DataBind();
        }
    }

    protected void g1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList ddlStates = (DropDownList)e.Row.FindControl("ddlstates");
            da = new SqlDataAdapter("Select distinct state from r where persontype like '%TON%' order by state",ConfigurationManager.ConnectionStrings["sSqlConn"].ConnectionString);
            DataSet ds = new DataSet();
            da.Fill(ds);
            ddlstates.DataSource = ds;
            ddlstates.DataTextField = "state";
            ddlstates.DataValueField = "state";

            ddlstates.DataBind();
            ListItem li = new ListItem();
            li.Text = "--Select State--";
            li.Value = "0";
            ddlstates.Items.Insert(0, li);
        }
    }
}
0
 
LVL 12

Expert Comment

by:Ramkisan Jagtap
ID: 36958527
what type of error u get on the page?
0
 

Author Comment

by:searchsanjaysharma
ID: 36959604
when i select state, and then district.Grid is not getting displayed. on district index change binddata() is called.
0
 
LVL 12

Expert Comment

by:Ramkisan Jagtap
ID: 36960006
I can't figure out your problem. Please try to debug your code. See what happen on

  protected void ddldistricts_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddldistricts.SelectedItem.Text != "--Select District--")
        {
            binddata();
        }

    }
0
 

Author Comment

by:searchsanjaysharma
ID: 36962087
The problem is there with g1_rowdatabound, when i remove it gridview is getting populated, but thr drop down is empty
0
 
LVL 12

Accepted Solution

by:
Ramkisan Jagtap earned 2000 total points
ID: 36962247
please check as below

  protected void g1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList ddlStates = (DropDownList)e.Row.FindControl("ddlstates");

          if(ddlStates!=NULL)
         {
            da = new SqlDataAdapter("Select distinct state from r where persontype like '%TON%' order by state",ConfigurationManager.ConnectionStrings["sSqlConn"].ConnectionString);
            DataSet ds = new DataSet();
            da.Fill(ds);
            ddlstates.DataSource = ds;
            ddlstates.DataTextField = "state";
            ddlstates.DataValueField = "state";

            ddlstates.DataBind();
            ListItem li = new ListItem();
            li.Text = "--Select State--";
            li.Value = "0";
            ddlstates.Items.Insert(0, li);
           }
        }
    }

0
 
LVL 12

Expert Comment

by:Ramkisan Jagtap
ID: 36962259
If possible please upload your page files i.e.(.aspx and .cs files)
0
 

Author Comment

by:searchsanjaysharma
ID: 36966011
Ya
Please file all files along with database contents in xlsx format. statedistrict.xlsx Default.aspx Default.aspx.cs Default.aspx statedistrict.xlsx
0
 

Author Closing Comment

by:searchsanjaysharma
ID: 37767162
ok
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Suggested Courses
Course of the Month12 days, 13 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question