Функция редактирования строк Gridview не работает
У Gridview есть 10 записей, и я собираюсь отредактировать 5-ю строку, когда она появится на edit click, но другие 5-10 записей не появятся(привязка)..
Я следую за столькими статьями, все еще одинаковыми только для того, что я написал код точно.
Пожалуйста, подскажите мне точное решение.
<asp:GridView ID="gvCustomerDetails" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" AllowPaging="true" PageSize="50" EmptyDataText="No data available" CssClass="gvDetails" AllowSorting="true" EnableEventValidation = "false" OnRowCancelingEdit="gvCustomerDetails_RowCancelingEdit" OnRowDataBound="gvCustomerDetails_RowDataBound" OnRowEditing="gvCustomerDetails_RowEditing" OnRowUpdating="gvCustomerDetails_RowUpdating"> <HeaderStyle BackColor="#c4c2c2" ForeColor="#000" HorizontalAlign="Left" Height="25" /> <Columns> <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" ItemStyle-Width="20" /> <asp:TemplateField ShowHeader="true" HeaderText="CustomerId" SortExpression="CustomerId"> <ItemTemplate> <%# Eval("CustomerId") %> <asp:Label ID="lblCustomerId" runat="server" Text='<%# Eval("CustomerId").ToString() %>' Visible="false"> </asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtCustomerId" runat="server" Text='<%# Eval("CustomerId") %>' MaxLength="7" /> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField ShowHeader="true" HeaderText="FirstName" SortExpression="FirstName"> <ItemTemplate> <%# Eval("FirstName") %> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtFirstName" runat="server" Text='<%# Eval("FirstName") %>' MaxLength="50"></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField ShowHeader="true" HeaderText="LastName" SortExpression="LastName"> <ItemTemplate> <%# Eval("LastName") %> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtLastName" runat="server" Text='<%# Eval("LastName") %>' MaxLength="50"></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField ShowHeader="true" HeaderText="EmailAddress" SortExpression="EmailAddress"> <ItemTemplate> <%# Eval("EmailAddress") %> </ItemTemplate> <EditItemTemplate> <asp:Label ID="lbl_orginal" runat="server" Text='<%#Eval("EmailAddress") %>' Style="display: none"></asp:Label> <asp:TextBox ID="txtEmailAddress" runat="server" Text='<%# Eval("EmailAddress") %>' MaxLength="50"></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField ShowHeader="true" HeaderText="Password"> <ItemTemplate> <%# Eval("Password") %> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtPassword" runat="server" Text='<%# Eval("Password") %>' MaxLength="10"></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField ShowHeader="true" HeaderText="JobTitle" SortExpression="JobTitle"> <ItemTemplate> <%# Eval("JobTitle") %> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtJobTitle" runat="server" Text='<%# Eval("JobTitle") %>' MaxLength="50"></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField ShowHeader="true" HeaderText="Company" SortExpression="Company"> <ItemTemplate> <%# Eval("Company") %> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtCompany" runat="server" Text='<%# Eval("Company") %>' MaxLength="50"></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField ShowHeader="true" HeaderText="Address" SortExpression="Address"> <ItemTemplate> <%# Eval("Address") %> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtAddress" runat="server" Text='<%# Eval("Address") %>' MaxLength="500"></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField ShowHeader="true" HeaderText="City" SortExpression="City"> <ItemTemplate> <%# Eval("City") %> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtCity" runat="server" Text='<%# Eval("City") %>' MaxLength="50"></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField ShowHeader="true" HeaderText="State" SortExpression="State"> <ItemTemplate> <%# Eval("State") %> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtState" runat="server" Text='<%# Eval("State") %>' MaxLength="50"></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField ShowHeader="true" HeaderText="Country" SortExpression="Country"> <ItemTemplate> <%# Eval("Country") %> </ItemTemplate> <EditItemTemplate> <asp:DropDownList ID="ddlCountry" runat="server" class="editableDDL" DataTextField="ddlCountry" DataValueField="ddlCountry" SelectedValue='<%# Eval("Country") %>'> <asp:ListItem Enabled="true" Text="NA" Value="NA"></asp:ListItem> <asp:ListItem Value="Afghanistan" Text="Afghanistan"></asp:ListItem> <asp:ListItem Value="Albania" Text="Albania"></asp:ListItem> <asp:ListItem Value="Algeria" Text="Algeria"></asp:ListItem> <asp:ListItem Value="Angola" Text="Angola"></asp:ListItem> <asp:ListItem Value="Argentina" Text="Argentina"></asp:ListItem> <asp:ListItem Value="Armenia" Text="Armenia"></asp:ListItem> <asp:ListItem Value="Australia" Text="Australia"></asp:ListItem> <asp:ListItem Value="Austria" Text="Austria"></asp:ListItem> <asp:ListItem Value="Azerbaijan" Text="Azerbaijan"></asp:ListItem> <asp:ListItem Value="Bahrain" Text="Bahrain"></asp:ListItem> <asp:ListItem Value="Bangladesh" Text="Bangladesh"></asp:ListItem> <asp:ListItem Value="Belarus" Text="Belarus"></asp:ListItem> <asp:ListItem Value="Belgium" Text="Belgium"></asp:ListItem> <asp:ListItem Value="Belize" Text="Belize"></asp:ListItem> <asp:ListItem Value="Bolivarian Republic of Venezuela" Text="Bolivarian Republic of Venezuela"></asp:ListItem> <asp:ListItem Value="Bolivia" Text="Bolivia"></asp:ListItem> <asp:ListItem Value="Bosnia and Herzegovina" Text="Bosnia and Herzegovina"></asp:ListItem> <asp:ListItem Value="Brazil" Text="Brazil"></asp:ListItem> <asp:ListItem Value="Brunei Darussalam" Text="Brunei Darussalam"></asp:ListItem> <asp:ListItem Value="Bulgaria" Text="Bulgaria"></asp:ListItem> <asp:ListItem Value="Cambodia" Text="Cambodia"></asp:ListItem> <asp:ListItem Value="Canada" Text="Canada"></asp:ListItem> <asp:ListItem Value="Caribbean" Text="Caribbean"></asp:ListItem> <asp:ListItem Value="Chile" Text="Chile"></asp:ListItem> <asp:ListItem Value="China" Text="China"></asp:ListItem> <asp:ListItem Value="Colombia" Text="Colombia"></asp:ListItem> <asp:ListItem Value="Costa Rica" Text="Costa Rica"></asp:ListItem> <asp:ListItem Value="Croatia" Text="Croatia"></asp:ListItem> <asp:ListItem Value="Cuba" Text="Cuba"></asp:ListItem> <asp:ListItem Value="Cyprus" Text="Cyprus"></asp:ListItem> <asp:ListItem Value="Czech Republic" Text="Czech Republic"></asp:ListItem> <asp:ListItem Value="Denmark" Text="Denmark"></asp:ListItem> <asp:ListItem Value="Dominican Republic" Text="Dominican Republic"></asp:ListItem> <asp:ListItem Value="Ecuador" Text="Ecuador"></asp:ListItem> <asp:ListItem Value="Egypt" Text="Egypt"></asp:ListItem> <asp:ListItem Value="El Salvador" Text="El Salvador"></asp:ListItem> <asp:ListItem Value="Estonia" Text="Estonia"></asp:ListItem> <asp:ListItem Value="Ethiopia" Text="Ethiopia"></asp:ListItem> <asp:ListItem Value="Faroe Islands" Text="Faroe Islands"></asp:ListItem> <asp:ListItem Value="Finland" Text="Finland"></asp:ListItem> <asp:ListItem Value="France" Text="France"></asp:ListItem> <asp:ListItem Value="Georgia" Text="Georgia"></asp:ListItem> <asp:ListItem Value="Germany" Text="Germany"></asp:ListItem> <asp:ListItem Value="Greece" Text="Greece"></asp:ListItem> <asp:ListItem Value="Greenland" Text="Greenland"></asp:ListItem> <asp:ListItem Value="Guatemala" Text="Guatemala"></asp:ListItem> <asp:ListItem Value="Honduras" Text="Honduras"></asp:ListItem> <asp:ListItem Value="Hong Kong S.A.R." Text="Hong Kong S.A.R."></asp:ListItem> <asp:ListItem Value="Hungary" Text="Hungary"></asp:ListItem> <asp:ListItem Value="Iceland" Text="Iceland"></asp:ListItem> <asp:ListItem Value="India" Text="India"></asp:ListItem> <asp:ListItem Value="Indonesia" Text="Indonesia"></asp:ListItem> <asp:ListItem Value="Iran" Text="Iran"></asp:ListItem> <asp:ListItem Value="Iraq" Text="Iraq"></asp:ListItem> <asp:ListItem Value="Ireland" Text="Ireland"></asp:ListItem> <asp:ListItem Value="Israel" Text="Israel"></asp:ListItem> <asp:ListItem Value="Italy" Text="Italy"></asp:ListItem> <asp:ListItem Value="Jamaica" Text="Jamaica"></asp:ListItem> <asp:ListItem Value="Japan" Text="Japan"></asp:ListItem> <asp:ListItem Value="Jordan" Text="Jordan"></asp:ListItem> <asp:ListItem Value="Kazakhstan" Text="Kazakhstan"></asp:ListItem> <asp:ListItem Value="Kenya" Text="Kenya"></asp:ListItem> <asp:ListItem Value="Korea" Text="Korea"></asp:ListItem> <asp:ListItem Value="Kuwait" Text="Kuwait"></asp:ListItem> <asp:ListItem Value="Kyrgyzstan" Text="Kyrgyzstan"></asp:ListItem> <asp:ListItem Value="Lao P.D.R." Text="Lao P.D.R."></asp:ListItem> <asp:ListItem Value="Latvia" Text="Latvia"></asp:ListItem> <asp:ListItem Value="Lebanon" Text="Lebanon"></asp:ListItem> <asp:ListItem Value="Libya" Text="Libya"></asp:ListItem> <asp:ListItem Value="Liechtenstein" Text="Liechtenstein"></asp:ListItem> <asp:ListItem Value="Lithuania" Text="Lithuania"></asp:ListItem> <asp:ListItem Value="Luxembourg" Text="Luxembourg"></asp:ListItem> <asp:ListItem Value="Macao S.A.R." Text="Macao S.A.R."></asp:ListItem> <asp:ListItem Value="Macedonia (FYROM)" Text="Macedonia (FYROM)"></asp:ListItem> <asp:ListItem Value="Malaysia" Text="Malaysia"></asp:ListItem> <asp:ListItem Value="Maldives" Text="Maldives"></asp:ListItem> <asp:ListItem Value="Malta" Text="Malta"></asp:ListItem> <asp:ListItem Value="Mexico" Text="Mexico"></asp:ListItem> <asp:ListItem Value="Mongolia" Text="Mongolia"></asp:ListItem> <asp:ListItem Value="Montenegro" Text="Montenegro"></asp:ListItem> <asp:ListItem Value="Morocco" Text="Morocco"></asp:ListItem> <asp:ListItem Value="Nepal" Text="Nepal"></asp:ListItem> <asp:ListItem Value="Netherlands" Text="Netherlands"></asp:ListItem> <asp:ListItem Value="New Zealand" Text="New Zealand"></asp:ListItem> <asp:ListItem Value="Nicaragua" Text="Nicaragua"></asp:ListItem> <asp:ListItem Value="Nigeria" Text="Nigeria"></asp:ListItem> <asp:ListItem Value="Norway" Text="Norway"></asp:ListItem> <asp:ListItem Value="Oman" Text="Oman"></asp:ListItem> <asp:ListItem Value="Pakistan" Text="Pakistan"></asp:ListItem> <asp:ListItem Value="Panama" Text="Panama"></asp:ListItem> <asp:ListItem Value="Paraguay" Text="Paraguay"></asp:ListItem> <asp:ListItem Value="Peru" Text="Peru"></asp:ListItem> <asp:ListItem Value="Philippines" Text="Philippines"></asp:ListItem> <asp:ListItem Value="Poland" Text="Poland"></asp:ListItem> <asp:ListItem Value="Portugal" Text="Portugal"></asp:ListItem> <asp:ListItem Value="Puerto Rico" Text="Puerto Rico"></asp:ListItem> <asp:ListItem Value="Qatar" Text="Qatar"></asp:ListItem> <asp:ListItem Value="Romania" Text="Romania"></asp:ListItem> <asp:ListItem Value="Russia" Text="Russia"></asp:ListItem> <asp:ListItem Value="Rwanda" Text="Rwanda"></asp:ListItem> <asp:ListItem Value="Saudi Arabia" Text="Saudi Arabia"></asp:ListItem> <asp:ListItem Value="Senegal" Text="Senegal"></asp:ListItem> <asp:ListItem Value="Serbia" Text="Serbia"></asp:ListItem> <asp:ListItem Value="Singapore" Text="Singapore"></asp:ListItem> <asp:ListItem Value="Slovakia" Text="Slovakia"></asp:ListItem> <asp:ListItem Value="Slovenia" Text="Slovenia"></asp:ListItem> <asp:ListItem Value="South Africa" Text="South Africa"></asp:ListItem> <asp:ListItem Value="Spain" Text="Spain"></asp:ListItem> <asp:ListItem Value="Sri Lanka" Text="Sri Lanka"></asp:ListItem> <asp:ListItem Value="Sudan" Text="Sudan"></asp:ListItem> <asp:ListItem Value="Sweden" Text="Sweden"></asp:ListItem> <asp:ListItem Value="Switzerland" Text="Switzerland"></asp:ListItem> <asp:ListItem Value="Syria" Text="Syria"></asp:ListItem> <asp:ListItem Value="Taiwan" Text="Taiwan"></asp:ListItem> <asp:ListItem Value="Tajikistan" Text="Tajikistan"></asp:ListItem> <asp:ListItem Value="Thailand" Text="Thailand"></asp:ListItem> <asp:ListItem Value="Tunisia" Text="Tunisia"></asp:ListItem> <asp:ListItem Value="Turkey" Text="Turkey"></asp:ListItem> <asp:ListItem Value="Turkmenistan" Text="Turkmenistan"></asp:ListItem> <asp:ListItem Value="U.A.E." Text="U.A.E."></asp:ListItem> <asp:ListItem Value="Ukraine" Text="Ukraine"></asp:ListItem> <asp:ListItem Value="United Kingdom" Text="United Kingdom"></asp:ListItem> <asp:ListItem Value="United States" Text="United States"></asp:ListItem> <asp:ListItem Value="Uruguay" Text="Uruguay"></asp:ListItem> <asp:ListItem Value="Uzbekistan" Text="Uzbekistan"></asp:ListItem> <asp:ListItem Value="Vietnam" Text="Vietnam"></asp:ListItem> <asp:ListItem Value="Yemen" Text="Yemen"></asp:ListItem> <asp:ListItem Value="Zimbabwe" Text="Zimbabwe"></asp:ListItem> </asp:DropDownList> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField ShowHeader="true" HeaderText="PhoneNumber" SortExpression="PhoneNumber"> <ItemTemplate> <%# Eval("PhoneNumber") %> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtPhoneNumber" runat="server" Text='<%# Eval("PhoneNumber") %>' MaxLength="25"></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField ShowHeader="true" HeaderText="UserType" SortExpression="UserType"> <ItemTemplate> <%# Eval("UserType") %> </ItemTemplate> <EditItemTemplate> <asp:DropDownList ID="ddlUserType" class="editableDDL" runat="server" DataTextField="ddlUserType" DataValueField="ddlUserType" SelectedValue='<%# Eval("UserType") %>'> <asp:ListItem Enabled="true" Text="NA" Value="NA"></asp:ListItem> <asp:ListItem Text="Tipper Tie Employee" Value="Tipper Tie Employee"></asp:ListItem> <asp:ListItem Text="Agent (Sales)" Value="Agent (Sales)"></asp:ListItem> <asp:ListItem Text="Agent (Technician)" Value="Agent (Technician)"></asp:ListItem> <asp:ListItem Text="End Customer" Value="End Customer"></asp:ListItem> <asp:ListItem Text="Portal Only" Value="Portal Only"></asp:ListItem> <asp:ListItem Text="Other" Value="Other"></asp:ListItem> </asp:DropDownList> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField ShowHeader="true" HeaderText="UserApprovalStatus" SortExpression="UserApprovalStatus"> <ItemTemplate> <%# Eval("UserApprovalStatus") %> </ItemTemplate> <EditItemTemplate> <asp:DropDownList ID="ddlUserApprovalStatus" class="editableDDL" runat="server" DataTextField="ddlUserApprovalStatus" DataValueField="ddlUserApprovalStatus" SelectedValue='<%# Eval("UserApprovalStatus") %>'> <asp:ListItem Enabled="true" Text="NA" Value="NA"></asp:ListItem> <asp:ListItem Text="Verified" Value="Verified"></asp:ListItem> <asp:ListItem Text="Not Verified" Value="Not Verified"></asp:ListItem> </asp:DropDownList> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField ShowHeader="true" HeaderText="UserAccountStatus" SortExpression="UserAccountStatus"> <ItemTemplate> <%# Eval("UserAccountStatus") %> </ItemTemplate> <EditItemTemplate> <asp:DropDownList ID="ddlUserAccountStatus" class="editableDDL" runat="server" DataTextField="ddlUserAccountStatus" DataValueField="ddlUserAccountStatus" SelectedValue='<%# Eval("UserAccountStatus") %>'> <asp:ListItem Enabled="true" Text="NA" Value="NA"></asp:ListItem> <asp:ListItem Text="Active" Value="Active"></asp:ListItem> <asp:ListItem Text="Inactive" Value="Inactive"></asp:ListItem> </asp:DropDownList> </EditItemTemplate> </asp:TemplateField> <asp:BoundField DataField="CurrentDate" DataFormatString="{0:MMMM dd, yyyy}" HtmlEncode="false" HeaderText="RegistrationDate" SortExpression="CurrentDate" ReadOnly="true" /> <asp:TemplateField ShowHeader="true" HeaderText="PrivilegedCategoriesList" SortExpression="PrivilegedCategoriesList"> <ItemTemplate> <%# Eval("PrivilegedCategoriesList") %> </ItemTemplate> <EditItemTemplate> <asp:ListBox ID="lbPrivilegedCategoriesList" class="editableLB" runat="server" DataTextField="lbPrivilegedCategoriesList" DataValueField="lbPrivilegedCategoriesList" SelectedValue='<%# Eval("PrivilegedCategoriesList") %>' SelectionMode="Multiple"> <asp:ListItem Enabled="true" Text="NA" Value="NA"></asp:ListItem> <asp:ListItem Text="Active" Value="Active"></asp:ListItem> <asp:ListItem Text="Inactive" Value="Inactive"></asp:ListItem> </asp:ListBox> </EditItemTemplate> </asp:TemplateField> <asp:CommandField ButtonType="Button" ShowEditButton="true" ShowCancelButton="true" /> </Columns> <FooterStyle BackColor="#c4c2c2" ForeColor="#000" HorizontalAlign="Left" Height="25" /> </asp:GridView>
public void gvCustomerDetailsBind() { SqlConnection con = new SqlConnection(strConnectionString); con.Open(); SqlCommand cmd = new SqlCommand("SELECT * FROM [CustomerLogin]", con); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); con.Close(); if (ds.Tables[0].Rows.Count > 0) { gvCustomerDetails.DataSource = ds; gvCustomerDetails.DataBind(); } else { ds.Tables[0].Rows.Add(ds.Tables[0].NewRow()); gvCustomerDetails.DataSource = ds; gvCustomerDetails.DataBind(); int columncount = gvCustomerDetails.Rows[0].Cells.Count; gvCustomerDetails.Rows[0].Cells.Clear(); gvCustomerDetails.Rows[0].Cells.Add(new TableCell()); gvCustomerDetails.Rows[0].Cells[0].ColumnSpan = columncount; gvCustomerDetails.Rows[0].Cells[0].Text = "No Records Found"; } } protected void gvCustomerDetails_RowEditing(object sender, GridViewEditEventArgs e) { try { gvCustomerDetails.EditIndex = e.NewEditIndex; gvCustomerDetailsBind(); } catch (Exception ex) { ex.Message.ToString(); } } protected void gvCustomerDetails_RowDataBound(object sender, GridViewRowEventArgs e) { SqlConnection con = new SqlConnection(strConnectionString); con.Open(); SqlCommand cmd = new SqlCommand("SELECT * FROM [CustomerLogin]", con); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); con.Close(); if (e.Row.RowType == DataControlRowType.DataRow) { Label lblType = (Label)e.Row.FindControl("Country"); if (lblType != null) { string typeId = lblType.Text; lblType.Text = ds.Tables[0].Rows[0]["Country"].ToString(); } DropDownList cmbType = (DropDownList)e.Row.FindControl("ddlCountry"); if (cmbType != null) { cmbType.DataSource = ds; cmbType.DataTextField = "ddlCountry"; cmbType.DataValueField = "ddlCountry"; cmbType.DataBind(); cmbType.SelectedValue = gvCustomerDetails.DataKeys[e.Row.RowIndex].Values[1].ToString(); } } }
Что я уже пробовал:
У Gridview есть 10 записей, и я собираюсь отредактировать 5-ю строку, когда она появится на edit click, но другие 5-10 записей не появятся(привязка)..
Suvendu Shekhar Giri
Вы пробовали отладить это?
Каковы ваши выводы в отладке?
jagan12013
Методы привязки данных, такие как Eval (), XPath () и Bind (), могут использоваться только в контексте элемента управления привязкой данных