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

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();
        }
    }
}

searchsanjaysharmaAsked:
Who is Participating?
 
Ramkisan JagtapLead DeveloperCommented:
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
 
guveraCommented:
Hi,
 
   Please check the below link.
 
   http://aspalliance.com/148_Merging_two_Datasets_into_a_single_Datagrid

 Regards
 Guvera
0
 
searchsanjaysharmaAuthor Commented:
soory no use
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
kswathiCommented:
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
 
Ramkisan JagtapLead DeveloperCommented:
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
 
Ramkisan JagtapLead DeveloperCommented:
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
 
searchsanjaysharmaAuthor Commented:
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
 
Ramkisan JagtapLead DeveloperCommented:
please add first 4 field in gridview as bound field and last one as template field in gridview.
0
 
searchsanjaysharmaAuthor Commented:

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
 
searchsanjaysharmaAuthor Commented:
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
 
Ramkisan JagtapLead DeveloperCommented:
what type of error u get on the page?
0
 
searchsanjaysharmaAuthor Commented:
when i select state, and then district.Grid is not getting displayed. on district index change binddata() is called.
0
 
Ramkisan JagtapLead DeveloperCommented:
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
 
searchsanjaysharmaAuthor Commented:
The problem is there with g1_rowdatabound, when i remove it gridview is getting populated, but thr drop down is empty
0
 
Ramkisan JagtapLead DeveloperCommented:
If possible please upload your page files i.e.(.aspx and .cs files)
0
 
searchsanjaysharmaAuthor Commented:
Ya
Please file all files along with database contents in xlsx format. statedistrict.xlsx Default.aspx Default.aspx.cs Default.aspx statedistrict.xlsx
0
 
searchsanjaysharmaAuthor Commented:
ok
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.