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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Applications

From novice to tech pro — start learning today.