从代码behinde将数据绑定到gridview中的DropDo

从代码behinde将数据绑定到gridview中的DropDo

本文介绍了如何从代码behinde将数据绑定到gridview中的DropDownList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将数据绑定到gridview中的dropdownlist,而不是sqldatasource后面的代码



我的代码是:



i cant bind data to dropdownlist in gridview from code behind without sqldatasource

my code is:

protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
            GetData();

    }
    public void GetData()
    {
        DataTable dt = new DataTable();
    string strSql  =   "SELECT    Tbl_Order.ID_Order,"+
                                " Tbl_Order.FlNm, "   +
                                " Tbl_Order.Email,"   +
                                " Tbl_Order.Sbj,"     +
                                " Tbl_Order.PhNo,"    +
                                " Tbl_Order.Psg,"     +
                                " Tbl_Order.FlUpld,"  +
                                " Tbl_Order.Srl_No,"  +
                                " Tbl_Prst.pursuit"   +
                       " FROM  Tbl_Order INNER JOIN Tbl_Prst ON Tbl_Order._id_prst = Tbl_Prst.id_prst";

        Dal.Fill_Dt(strSql, dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        DataTable dt_pursuit = new DataTable();
        Dal.Fill_Dt("select _id_prst,pursuit from Tbl_Prst)", dt_pursuit);

        string FlNm = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtFlNm")).Text;
        string Email = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEmail")).Text;
        string Sbj = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtSbj")).Text;
        string PhNo = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtPhNo")).Text;
        string Psg = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtPsg")).Text;
        string Srl_No = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtSrl_No")).Text;
        string pursuit = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtpursuit")).Text;
        string id = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtID_Order")).Text;

        ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DDLpursuit")).DataSource = dt_pursuit;
        ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DDLpursuit")).DataTextField = "pursuit";
        ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DDLpursuit")).DataValueField= "_id_prst";
        ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DDLpursuit")).DataBind();

        string str = string.Format("update Tbl_Order set FlNm=N'{0}',Email=N'{1}',Sbj=N'{2}',PhNo=N'{3}',Psg=N'{4}',Srl_No=N'{5}'" +
                                   "WHERE ID_Order={6}", FlNm, Email, Sbj, PhNo, Psg, Srl_No, id);
        Dal.Cmd_Qry(str);

        GridView1.EditIndex = -1;
        GetData();







<asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>

                <asp:GridView ID="GridView1"

                    AutoGenerateColumns="False"

                    OnRowUpdating="GridView1_RowUpdating"

                    OnRowEditing="GridView1_RowEditing"

                    OnRowCancelingEdit="GridView1_RowCancelingEdit"

                    OnRowDeleting="GridView1_RowDeleting"

                    DataKeyNames="ID_Order"

                    runat="server">
                    <Columns>
                        <asp:CommandField ShowEditButton="True" />

                        <asp:TemplateField>
                            <EditItemTemplate>
                                <asp:TextBox ID="txtID_Order" ReadOnly="true"  runat="server" Text='<%# Bind("ID_Order") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="lblID_Order" runat="server" Text='<%# Bind("ID_Order") %>'>></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField>
                            <EditItemTemplate>
                                <asp:TextBox ID="txtFlNm" runat="server" Text='<%# Bind("FlNm") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="lblFlNm" runat="server" Text='<%# Bind("FlNm") %>'>></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>

.......
.........
............





i希望将代码后面的数据绑定到dropdownlist,如下所示/>




i want bind data from code behind to dropdownlist like this

public void ShowDDL_pursuit()
    {
        DataTable dt_pursuit = new DataTable();
        Dal.Fill_Dt("select _id_prst,pursuit from Tbl_Prst)", dt_pursuit);
        DDLpursuit.DataSource = dt_pursuit;
        DDLpursuit.DataTextField = "pursuit";
        DDLpursuit.DataValueField = "_id_prst";
        DDLpursuit.DataBind();

    }





但是在课堂上并不知道

我试试这个:



but is not unknown in class
and i try this:

((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DDLpursuit")).DataSource = dt_pursuit;
       ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DDLpursuit")).DataTextField = "pursuit";
       ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DDLpursuit")).DataValueField= "_id_prst";
       ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DDLpursuit")).DataBind();





但它不起作用

请帮帮我怎样才能做到这一点



but it''s not work
please help me how can i do this

推荐答案




这篇关于如何从代码behinde将数据绑定到gridview中的DropDownList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 22:34