2014-04-18 22 views
0

我試圖在網格中添加一個新的行,下拉列表作爲其中的一列。現在即時嘗試將數據綁定到下拉列表添加新行但數據不顯示。任何人都可以告訴我什麼是我的代碼的問題?綁定下拉網格新行中的數據

<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 


    } 
+0

'obj.ShowAllAccounts()'是否返回任何值? – LakshmiNarayanan

+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++; 
       } 
      } 
     } 
    }