开发者

How to get selected value for dropdown list inside gridview?

Scenarios

I am using DataGrid with a column containing DropDownList. When I update the values, the DropDownList value does not get updated.

My Research so far

I believe it needs some C# code to made the DropDownList editable. I don't know where to put the code and how? Is there a shorter way than C# code?

Thank you very much

Edit : Supplied Code

student_table : id, name, major, favorite_teacher

teacher_table : id, name

The gridview has datasource of student. The DropDownList has datasource of teacher.

In the gridview with, I have made favorite_teacher as template field (DropDownList) and has teacher_table as datasource. In the edit mode, the DropDownList shows correct and populates teachers from the teacher table. When 开发者_如何学JAVAI select a favourite teacher for any student and click submit, the submit does not go through. I might need some C# code or otherwise. Don't know how to fix this problem.


Please try the below code and let me know if it's fixed:

protected void btnClick_Click(object sender, EventArgs e)
{
    var tmpValue = ((DropDownList)gvRowItem.Cells[/*cellId*/].FindControl("/*dropDownListId*/")).SelectedValue;
}


You can try the following code

DataSourceID="sds_teacher_table" is referring to the teacher_table. the gridview will be referring to the student_table

            <asp:TemplateField HeaderText="Favourite Teacher" >
                <ItemTemplate>
                    <asp:Label ID="lblfavorite_teacher" runat="server" Text='<%# LookupTeacherName(DataBinder.Eval(Container.DataItem, "id")) %>'></asp:Label> 
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="ddlfavorite_teacher" runat="server" DataSourceID="sds_teacher_table" DataTextField="name" DataValueField="id" SelectedValue='<%#Bind("id")%>' Width="98%">
                    </asp:DropDownList> 
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:DropDownList ID="ddlNewfavorite_teacher" runat="server" DataSourceID="sds_teacher_table" DataTextField="name" DataValueField="id" SelectedValue='<%#Bind("id")%>' Width="95%">
                    </asp:DropDownList> 
                </FooterTemplate>
               <ItemStyle Width="25%" /> 
           </asp:TemplateField>


    protected string LookupTeacherName(object idObj)
    {
        if (string.IsNullOrEmpty(idObj.ToString()))
            return null;

        string TeacherId = idObj.ToString();

        // find the corresponding name
        IEnumerator enumos = sds_teacher_table.Select(new DataSourceSelectArguments()).GetEnumerator();
        while (enumos.MoveNext())
        {
            DataRowView row = enumos.Current as DataRowView;

            if ((string)row["id"].ToString() == TeacherId)
                return string.Concat(row["name"].ToString());
        }

        return TeacherId;
    }


I implemented this from a tutorial here without writing any C# code.

You do not need C# code, although you can implement it using C# code as well.

There is a trick that you have to implement. You need to bind the values of gridview to a datasource of the other table that you want to fetch data from. But by design, since dropdown list is contained in GridView, it can only be bound to gridview fields. This is the main complication.

To overcome that, include those additional fields in the gridview. You may need to use join statement so that you can include all the fields that you need in gridview. Now you can easily bind those fields to DropDownlist. Of course you do not need to show these extra values in GridView, they are there only for DropDownList. This will solve your problem, without writing a single line of code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜