Dropdown list in GridView c#

Hi, thanks for looking at my question.

I am building a web application and am using a gridview to show data and allow the user to change this data.

I have my gridview and within it I have a dropdown list that shows data from an SQL table, the dropdown list's datasource is set programatically (see code below) and this works fine.

At the moment the dropdown list shows everything in the table but I would like the selected value to be what is in the datagrid data.

Please see the attached code and screenshot.

(the screenshot shows the datagrid showing the data but the dropdown list is showing UK Helpdesk which is the first record in the lookup table, this needs to show the value from the datagrod table)

I hope this makes sense, if not, please ask me any questions

thanks

PDM
<asp:TemplateField HeaderText="Group">
                        <ItemTemplate>
                            <asp:DropDownList ID="ddlGroup" runat="server" DataSource=<%#PopulateGroups() %> DataTextField="Description" DataValueField="Group_ID"  />
                        </ItemTemplate>
                    </asp:TemplateField>



public DataTable PopulateGroups()
    {
        string sConnString = WebConfigurationManager.ConnectionStrings["HelpdeskConnectionString"].ConnectionString;
        SqlConnection connSQLH = new SqlConnection(sConnString);
    
        SqlDataAdapter adGroup = new SqlDataAdapter("SELECT Group_ID, Description FROM HD_LU_GROUPS WHERE (Lang_Code = '" + Session["Language"].ToString() + "')", connSQLH);
        DataTable dtG = new DataTable();
        adGroup.Fill(dtG);
           
        return dtG;
        
        
    }

Open in new window

dataGrid-and-Dropdown-list.JPG
PigdogmonsterAsked:
Who is Participating?
 
RosenetCommented:
My apologies the code attachment seems to have been lost...here it is.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCreated="GridView1_RowCreated">
        <Columns>
        <asp:BoundField HeaderText="Parent" DataField="Parent" />
        <asp:BoundField DataField="ParentChild" Visible="false" />
        <asp:TemplateField HeaderText="Groups">
            <ItemTemplate>
                <asp:DropDownList ID="ddlGroups" runat="server"  />
            </ItemTemplate>
        </asp:TemplateField>
        </Columns>
        </asp:GridView>

        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Parent");
            dt.Columns.Add("ParentChild");
            dt.Columns.Add("SubGroups");

            DataRow dr = dt.NewRow();
            dr["Parent"] = "Parent 1";
            dr["ParentChild"] = "3";
            dr["SubGroups"] = Groups();
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["Parent"] = "Parent 2";
            dr["ParentChild"] = "1";
            dr["SubGroups"] = Groups();
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["Parent"] = "Parent 3";
            dr["ParentChild"] = "1";
            dr["SubGroups"] = Groups();
            dt.Rows.Add(dr);

            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

        public DataTable Groups()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("GroupID", typeof(Int32));
            dt.Columns.Add("Description", typeof(string));

            DataRow dr = dt.NewRow();
            dr["GroupID"] = 1;
            dr["Description"] = "Group 1";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["GroupID"] = 2;
            dr["Description"] = "Group 2";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["GroupID"] = 3;
            dr["Description"] = "Group 3";
            dt.Rows.Add(dr);

            return dt;
        }

        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DropDownList ddl = (DropDownList)e.Row.FindControl("ddlGroups");
                ddl.DataValueField = "GroupID";
                ddl.DataTextField = "Description";
                ddl.DataSource = Groups();
                ddl.DataBind();
                ddl.SelectedValue = ((DataRowView)e.Row.DataItem)["ParentChild"].ToString();
            }
        }

Open in new window

0
 
RosenetCommented:
Hello there,

Please see code attached and let me know if that helps solve your problem.  It is all done programmatically which tends to clean up the process a bit.

Essentially you bind the grid to a dataset (I just created one from scratch - replace that with your SQL dataset), and then after the DropDownLists are created, select the value you want from a hidden column in the GridView's DataSource.
0
 
PigdogmonsterAuthor Commented:
Hi Rosenet,

Thank you for you comments.

As you can probably tell, I am very new to c# so can you tell me where exaclty do I put my SQL code?

thanks

Stuart
0
 
RosenetCommented:
Hi Stuart,

In the Page_Load method I am creating dummy data (you didn't show how you get your data in your original post).  And the Groups() method is creating sub-dummy data for the dropdown(s).

In your example you have a data source for your grid so that stays the same, the only difference is the Groups() method in my example is your PopulateGroups() method.  You can see they both return a DataTable, the only difference is how you use it to populate the grid.
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.