2017-01-05 51 views
0

我在gridview中有兩列Customer TypeFile Frequency。當網格處於正常模式時,我使用標籤顯示值。在編輯行時,這兩列變成了下拉菜單。我使用OnRowDataBound="RowDataBound"綁定下拉列表。但是,RowDataBound方法中的第一個下拉列表(以方法中的第一個寫入)只能在編輯模式下綁定。無法在gridview中編輯一行時綁定多個下拉列表

的.aspx

<asp:GridView ID="gvManageCustomers" DataKeyNames="Ship_To" runat="server" AutoGenerateColumns="False" 
    OnRowEditing="EditCustomer" OnRowDataBound="RowDataBound" OnRowUpdating="UpdateCustomer" 
    OnRowCancelingEdit="CancelEdit" CssClass="table table-bordered table-condensed"> 
    <Columns> 
     <asp:TemplateField HeaderText="Customer Type"> 
      <ItemTemplate> 
       <asp:Label ID="lblCustType" runat="server" Text='<%# Eval("Customer_Type")%>'></asp:Label> 
      </ItemTemplate> 
      <EditItemTemplate> 
       <asp:Label ID="lblCustType" runat="server" Text='<%# Eval("Customer_Type")%>' Visible="false"> 
       </asp:Label> 
       <asp:DropDownList ID="ddlgvCustomerType" runat="server"> 
       </asp:DropDownList> 
      </EditItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="File Frequency"> 
      <ItemTemplate> 
       <asp:Label ID="lblFileFreq" runat="server" Text='<%# Eval("FileFrequency")%>'></asp:Label> 
      </ItemTemplate> 
      <EditItemTemplate> 
       <asp:Label ID="lblFileFreq" runat="server" Text='<%# Eval("FileFrequency")%>' Visible="false"></asp:Label> 
       <asp:DropDownList ID="ddlgvFileFreq" runat="server"> 
       </asp:DropDownList> 
      </EditItemTemplate> 
     </asp:TemplateField> 
     <asp:CommandField ShowEditButton="True" /> 
    </Columns> 
</asp:GridView> 

的.cs

public DataTable FetchCustomerType() 
{ 
    string sql = "select distinct Customer_TypeID,Customer_Type from tbl_CustomerType"; 
    SqlDataAdapter da = new SqlDataAdapter(sql, constr); 
    DataTable dt = new DataTable(); 
    da.Fill(dt); 
    return dt; 
} 
public DataTable FetchFileFrequency() 
{ 
    string sql = "SELECT distinct FileFrequency_ID,FileFrequency FROM [tbl_FileFrequency]"; 
    SqlDataAdapter da = new SqlDataAdapter(sql, constr); 
    DataTable dt = new DataTable(); 
    da.Fill(dt); 
    return dt; 
} 
protected void RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    try 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      if ((e.Row.RowState & DataControlRowState.Edit) > 0) 
      { 
       DropDownList ddlgvFileFreq = (DropDownList)e.Row.FindControl("ddlgvFileFreq"); //getting binded 
       ddlgvFileFreq.DataSource = FetchFileFrequency(); 
       ddlgvFileFreq.DataTextField = "FileFrequency"; 
       ddlgvFileFreq.DataValueField = "FileFrequency_ID"; 
       ddlgvFileFreq.DataBind(); 
       ddlgvFileFreq.Items.FindByValue((e.Row.FindControl("lblFileFreq") as Label).Text).Selected = true; 

       DropDownList ddlgvCustomerType = (DropDownList)e.Row.FindControl("ddlgvCustomerType"); 
       ddlgvCustomerType.DataSource = FetchCustomerType(); 
       ddlgvCustomerType.DataTextField = "Customer_Type"; 
       ddlgvCustomerType.DataValueField = "Customer_TypeID"; 
       ddlgvCustomerType.DataBind(); 
       ddlgvCustomerType.Items.FindByValue((e.Row.FindControl("lblCustType") as Label).Text).Selected = true; 

      } 
     } 

    } 
    catch (Exception ex) 
    { 
     //log error 
     errorlog.WriteErrorLog(ex.ToString()); 
    } 
} 
+0

我建議綁定'DDLs'在'OnRowEditing'事件。 – Null

回答

0

的問題是在這條線,它trows一個exeption。這就是爲什麼只有最頂層的綁定才能起作用。

ddlgvFileFreq.Items.FindByValue((e.Row.FindControl("lblFileFreq") as Label).Text).Selected = true; 

但它看起來像你想讓DropDownList有正確的SelectedValue。這是你如何做到的。

DataRowView row = e.Row.DataItem as DataRowView; 

DropDownList ddlgvFileFreq = (DropDownList)e.Row.FindControl("ddlgvFileFreq"); 
ddlgvFileFreq.DataSource = FetchFileFrequency(); 
ddlgvFileFreq.DataTextField = "FileFrequency"; 
ddlgvFileFreq.DataValueField = "FileFrequency_ID"; 
ddlgvFileFreq.DataBind(); 
try 
{ 
    ddlgvFileFreq.SelectedValue = row["FileFrequency_ID"].ToString(); 
} 
catch 
{ 
} 

DropDownList ddlgvCustomerType = (DropDownList)e.Row.FindControl("ddlgvCustomerType"); 
ddlgvCustomerType.DataSource = FetchCustomerType(); 
ddlgvCustomerType.DataTextField = "Customer_Type"; 
ddlgvCustomerType.DataValueField = "Customer_TypeID"; 
ddlgvCustomerType.DataBind(); 
try 
{ 
    ddlgvCustomerType.SelectedValue = row["Customer_TypeID"].ToString(); 
} 
catch 
{ 
} 
+0

它沒有拋出任何異常。我試過你的解決方案,仍然是相同的問題 – Raviteja

+0

當你刪除try catch塊時會發生什麼?因爲測試時它工作得很好。如果你在'ddlgvFileFreq.SelectedValue'上得到一個錯誤,這意味着你用來綁定網格和下拉的數據集不匹配。 – VDWWD

+0

錯誤爲'Uncaught Error:Sys.WebForms.PageRequestManagerServerErrorException:FileFrequency_ID既不是DataColumn也不是DataRelation for ...( – Raviteja

-1
<asp:TemplateField HeaderText="Customer Type"> 
            <ItemTemplate> 
             <asp:Label ID="lblCustType" runat="server" Text='<%# Eval("Customer_Type")%>'></asp:Label> 
             <asp:Label ID="lblCustTypeID" runat="server" Visible="false" Text='<%# (Eval("Customer_TypeID")) %>' /> 
            </ItemTemplate> 
            <EditItemTemplate>          
             <asp:DropDownList ID="ddlgvCustomerType" runat="server" DataSource="<%# FetchCustomerType() %>" DataValueField="Customer_TypeID" DataTextField="Customer_Type" > 
             </asp:DropDownList> 
            </EditItemTemplate> 
           </asp:TemplateField> 
           <asp:TemplateField HeaderText="File Frequency"> 
            <ItemTemplate> 
             <asp:Label ID="lblFileFreq" runat="server" Text='<%# Eval("FileFrequency")%>'></asp:Label> 
             <asp:Label ID="lblFileFreqID" runat="server" Visible="false" Text='<%# (Eval("FileFrequency_ID")) %>' /> 
            </ItemTemplate> 
            <EditItemTemplate>          
             <asp:DropDownList ID="ddlgvFileFreq" runat="server" DataSource="<%# FetchFileFrequency() %>" DataValueField="FileFrequency_ID" DataTextField="FileFrequency" > 
             </asp:DropDownList> 
            </EditItemTemplate> 
           </asp:TemplateField> 

在onRowEditing事件

((DropDownList)gvManageCustomers.Rows[e.NewEditIndex].Cells[0].FindControl("ddlgvCustomerType")).SelectedValue = ((Label)gvManageCustomers.Rows[e.NewEditIndex].Cells[0].FindControl("lblCustTypeID")).Text; 
          ((DropDownList)gvManageCustomers.Rows[e.NewEditIndex].Cells[0].FindControl("ddlgvFileFreq")).SelectedValue = ((Label)gvManageCustomers.Rows[e.NewEditIndex].Cells[0].FindControl("lblFileFreqID")).Text; 
相關問題