使用对象数据源在Gridview中获取下拉列表更新值

时间:2016-09-04 17:23:45

标签: c# asp.net gridview

我有一个gridview,其中一列包含下拉列表。下拉列表中的项目来自 daftarproperti.cs (对象数据源类)。 这里的问题是每当我更新行时,此下拉列表的更新值变为0。

这是我的aspx的摘录:

                    <asp:GridView ID="gv_dalamlokasi_jadwal"
                    CssClass="table table-bordered"
                    runat="server"
                    BackColor="White"
                    BorderColor="#CCCCCC"
                    BorderStyle="None"
                    BorderWidth="1px"
                    CellPadding="3"
                    DataKeyNames="tugasID"
                    ShowFooter="True"
                    OnRowDataBound="gv_dalamlokasi_jadwal_OnRowDataBound"

                    AutoGenerateColumns="False" DataSourceID="ds_dalamlokasi_rincian">
                    <Columns> <asp:TemplateField HeaderText="Peran" SortExpression="Peran">
                            <EditItemTemplate>
                                <asp:DropDownList ID="ddl_peran"
                                    runat="server"

                                    DataTextField="nmperan"
                                    DataValueField="kdperan">
                                </asp:DropDownList>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="lbl_peran" runat="server" Text='<%# Eval("nmperan") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>

这是aspx.cs代码:

protected void gv_dalamlokasi_jadwal_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        //DataRowView dRowView = (DataRowView)e.Row.DataItem;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if ((e.Row.RowState & DataControlRowState.Edit) > 0)
            {
                DropDownList ddlperan = (DropDownList)e.Row.FindControl("ddl_peran");
                if (ddlperan != null)
                {
                    ddlperan.DataSource = daftarproperti.TampilkanPeran();
                    ddlperan.DataValueField = "kdperan";
                    ddlperan.DataTextField = "nmperan";
                    ddlperan.DataBind();
                    DataRowView dr = e.Row.DataItem as DataRowView;
                    //ddList.SelectedItem.Text = dr["category_name"].ToString();
                    ddlperan.SelectedValue = dr["kdperan"].ToString();
                }
            }
        }
    }

这是daftarproperti.cs代码:

public class daftarproperti
{public static List<daftarproperti> TampilkanPeran()
    {
        List<daftarproperti> Listperan = new List<daftarproperti>();
        string CS = ConfigurationManager.ConnectionStrings["nikita_app"].ConnectionString;

        using (SqlConnection con = new SqlConnection(CS))
        {
            SqlCommand cmd = new SqlCommand("SELECT*FROM [nikitaapp].[dbo].[vt_peran]", con);
            con.Open();
            SqlDataReader rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                daftarproperti bacaperan = new daftarproperti();
                bacaperan.kdperan = (int)rdr["kdperan"];
                bacaperan.nmperan = rdr["nmperan"].ToString();
                Listperan.Add(bacaperan);
            }
        }

        return Listperan;

    }

如何在更新时使代码正常工作?

0 个答案:

没有答案