使用新行网格中的数据绑定下拉列表

时间:2014-04-18 10:29:12

标签: c# asp.net .net grid

我尝试在网格中添加一个新行,其中包含下拉列表。现在我尝试在添加新行但数据没有显示时将数据绑定到下拉列表。谁能告诉我我的代码有什么问题?

    <asp:GridView ID="grdJournal" runat="server" ShowFooter="true" AutoGenerateColumns="false">
                        <Columns>
                            <asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
                            <asp:TemplateField HeaderText="Date">
                                <ItemTemplate>
                                    <asp:TextBox ID="lblDate" runat="server"></asp:TextBox>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Account">
                                <ItemTemplate>
                                    <asp:DropDownList ID="ddlAccounts" runat="server"></asp:DropDownList>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Description">
                                <ItemTemplate>
                                    <asp:TextBox ID="lblDescription" runat="server"></asp:TextBox>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Debit">
                                <ItemTemplate>
                                    <asp:TextBox ID="lblDebit" runat="server"></asp:TextBox>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Credit">
                                <ItemTemplate>
                                    <asp:TextBox ID="lblCredit" runat="server"></asp:TextBox>
                                </ItemTemplate>

                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>

我的代码隐藏就是这个

 private void SetInitialRow()
    {
        DataTable dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
        dt.Columns.Add(new DataColumn("colDate", typeof(string)));
        dt.Columns.Add(new DataColumn("colAccount", typeof(string)));
        dt.Columns.Add(new DataColumn("colDescription", typeof(string)));
        dt.Columns.Add(new DataColumn("colDebit", typeof(string)));
        dt.Columns.Add(new DataColumn("colCredit", typeof(string)));
        dr = dt.NewRow();
        dr["RowNumber"] = 1;
        dr["colDate"] = string.Empty;
        dr["colAccount"] = string.Empty;
        dr["colDescription"] = string.Empty;
        dr["colDebit"] = string.Empty;
        dr["colCredit"] = string.Empty;
        dt.Rows.Add(dr);

        //Store the DataTable in ViewState
        ViewState["CurrentTable"] = dt;

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

    private void AddNewRowToGrid()
    {
        int rowIndex = 0;

        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow = null;
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                    //extract the TextBox values
                    TextBox lblDate = (TextBox)grdJournal.Rows[rowIndex].Cells[1].FindControl("lblDate");
                    DropDownList lblAccount = (DropDownList)grdJournal.Rows[rowIndex].Cells[2].FindControl("ddlAccounts");
                    TextBox lblDescription = (TextBox)grdJournal.Rows[rowIndex].Cells[3].FindControl("lblDescription");
                    TextBox lblDebit = (TextBox)grdJournal.Rows[rowIndex].Cells[3].FindControl("lblDebit");
                    TextBox lblCredit = (TextBox)grdJournal.Rows[rowIndex].Cells[3].FindControl("lblCredit");
                    Classes.CRUD obj = new Classes.CRUD();
                    lblAccount.DataSource = obj.ShowAllAccounts();
                    lblAccount.DataTextField = "Key";
                    lblAccount.DataValueField = "Value";
                    lblAccount.DataBind();
                    drCurrentRow = dtCurrentTable.NewRow();
                    drCurrentRow["RowNumber"] = i + 1;
                    //Setting Values to new Row


                    dtCurrentTable.Rows[i - 1]["colDate"] = lblDate.Text;
                    dtCurrentTable.Rows[i - 1]["colAccount"] = lblAccount.SelectedItem.Text;
                    dtCurrentTable.Rows[i - 1]["colDescription"] = lblDescription.Text;
                    dtCurrentTable.Rows[i - 1]["colDebit"] = lblDebit.Text;
                    dtCurrentTable.Rows[i - 1]["colCredit"] = lblCredit.Text;

                    rowIndex++;
                }
                dtCurrentTable.Rows.Add(drCurrentRow);

                ViewState["CurrentTable"] = dtCurrentTable;

                grdJournal.DataSource = dtCurrentTable;
                grdJournal.DataBind();
            }
        }
        else
        {
            Response.Write("ViewState is null");
        }

        //Set Previous Data on Postbacks
        SetPreviousData();


    }
    private void SetPreviousData()
    {
        int rowIndex = 0;
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dt = (DataTable)ViewState["CurrentTable"];
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    TextBox lblDate = (TextBox)grdJournal.Rows[rowIndex].Cells[1].FindControl("lblDate");
                    DropDownList lblAccount = (DropDownList)grdJournal.Rows[rowIndex].Cells[2].FindControl("ddlAccounts");
                    TextBox lblDescription = (TextBox)grdJournal.Rows[rowIndex].Cells[3].FindControl("lblDescription");
                    TextBox lblDebit = (TextBox)grdJournal.Rows[rowIndex].Cells[3].FindControl("lblDebit");
                    TextBox lblCredit = (TextBox)grdJournal.Rows[rowIndex].Cells[3].FindControl("lblCredit");

                    lblDate.Text = dt.Rows[i]["colDate"].ToString();
               //     lblAccount.Text = dt.Rows[i]["colAccount"].ToString();
                    lblDescription.Text = dt.Rows[i]["colDescription"].ToString();
                    lblDebit.Text = dt.Rows[i]["colDebit"].ToString();
                    lblCredit.Text = dt.Rows[i]["colCredit"].ToString();

                    rowIndex++;
                }
            }
        }
    }
 protected void btnSubmit_Click(object sender, EventArgs e)
    {
        AddNewRowToGrid();
    }
 protected void Page_Load(object sender, EventArgs e)
    {
        Classes.CRUD obj = new Classes.CRUD();
        #region Binding Events

        if (!Page.IsPostBack)
        {
            SetInitialRow();

            ddlType.DataTextField = "Value";
            ddlType.DataValueField = "Key";
            ddlType.DataSource = obj.ShowTypesOfTransactions();
            ddlType.DataBind();
        }

        #endregion


    }

1 个答案:

答案 0 :(得分:1)

解决了它。

  private void SetPreviousData()
    {
        int rowIndex = 0;
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dt = (DataTable)ViewState["CurrentTable"];
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    TextBox TextBoxName = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[1].FindControl("txtName");
                    TextBox TextBoxAge = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[2].FindControl("txtAge");
                    TextBox TextBoxAddress =
                      (TextBox)grvStudentDetails.Rows[rowIndex].Cells[3].FindControl("txtAddress");
                    RadioButtonList RBLGender =
                      (RadioButtonList)grvStudentDetails.Rows[rowIndex].Cells[4].FindControl("RBLGender");
                    DropDownList DrpQualification =
                      (DropDownList)grvStudentDetails.Rows[rowIndex].Cells[5].FindControl("drpQualification");

                    //Added these lines

                    Classes.CRUD obj = new Classes.CRUD();
                    DrpQualification.DataSource = obj.ShowAllAccounts();
                    DrpQualification.DataBind();

                    //****************
                    TextBoxName.Text = dt.Rows[i]["Col1"].ToString();
                    TextBoxAge.Text = dt.Rows[i]["Col2"].ToString();
                    TextBoxAddress.Text = dt.Rows[i]["Col3"].ToString();
                    RBLGender.SelectedValue = dt.Rows[i]["Col4"].ToString();
                    DrpQualification.SelectedValue = dt.Rows[i]["Col5"].ToString();
                    rowIndex++;
                }
            }
        }
    }