locked
edit data of gridview RRS feed

  • Question

  • User-1092307275 posted

    I have a gridview with 3 dropdown list. now I encounter problems during the run and edit my gridview

    this is my gridview code:

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="SqlDataSource1">
                <Columns>
                    <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
                    <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" />
                    <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
                    <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
                    <asp:BoundField DataField="BirthDate" HeaderText="BirthDate" SortExpression="BirthDate" />
                    <asp:BoundField DataField="BirthPlace" HeaderText="BirthPlace" SortExpression="BirthPlace" />
                    <asp:TemplateField HeaderText="Gender" SortExpression="Gender">
                        <EditItemTemplate>
                            <asp:DropDownList ID="DropDownList1" runat="server">
                                <asp:ListItem Value="0">زن</asp:ListItem>
                                <asp:ListItem Value="1">مرد</asp:ListItem>
                            </asp:DropDownList>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Bind("Gender") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="ReligionId" SortExpression="ReligionId">
                        <EditItemTemplate>
                            <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="Religion" DataTextField="Title" DataValueField="Title">
                            </asp:DropDownList>
                            <asp:SqlDataSource ID="Religion" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
                                SelectCommand="SELECT [Title] FROM [IMS_Religion]"></asp:SqlDataSource>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label2" runat="server" Text='<%# Bind("ReligionId") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="MaritalId" SortExpression="MaritalId">
                        <EditItemTemplate>
                            <asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="Marital" DataTextField="Status" DataValueField="Status">
                            </asp:DropDownList>
                            <asp:SqlDataSource ID="Marital" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
                                SelectCommand="SELECT [Status] FROM [IMS_Marital]"></asp:SqlDataSource>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label3" runat="server" Text='<%# Bind("MaritalId") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
               DeleteCommand="DELETE FROM [IMS_Person] WHERE [Id] = @Id"
               InsertCommand="INSERT INTO [IMS_Person] ([GuidId], [FirstName], [LastName], [BirthDate], [BirthPlace], [Gender], [ReligionId], [MaritalId])
               VALUES (@GuidId, @FirstName, @LastName, @BirthDate, @BirthPlace, @Gender, @ReligionId, @MaritalId)"
               SelectCommand="SELECT * FROM [IMS_Person]"
               UpdateCommand="UPDATE [IMS_Person] SET [FirstName] = @FirstName, [LastName] = @LastName, [BirthDate] = @BirthDate,
               [BirthPlace] = @BirthPlace, [Gender] = @Gender, [ReligionId] = @ReligionId, [MaritalId] = @MaritalId WHERE [Id] = @Id"
               OnUpdating="SqlDataSource1_Updating"
               >
               <DeleteParameters>
                   <asp:Parameter Name="Id" Type="Int32" />
               </DeleteParameters>
               <InsertParameters>
                   <asp:Parameter Name="FirstName" Type="String" />
                   <asp:Parameter Name="LastName" Type="String" />
                   <asp:Parameter Name="BirthDate" Type="DateTime" />
                   <asp:Parameter Name="BirthPlace" Type="String" />
                   <asp:Parameter Name="Gender" Type="Byte" />
                   <asp:Parameter Name="ReligionId" Type="Byte" />
                   <asp:Parameter Name="MaritalId" Type="Byte" />
               </InsertParameters>
               <UpdateParameters>
                   <asp:Parameter Name="FirstName" Type="String" />
                   <asp:Parameter Name="LastName" Type="String" />
                   <asp:Parameter Name="BirthDate" Type="DateTime" />
                   <asp:Parameter Name="BirthPlace" Type="String" />
                   <asp:Parameter Name="Gender" Type="Byte" />
                   <asp:Parameter Name="ReligionId" Type="Byte" />
                   <asp:Parameter Name="MaritalId" Type="Byte" />
                   <asp:Parameter Name="Id" Type="Int32" />
               </UpdateParameters>
           </asp:SqlDataSource>
    protected void SqlDataSource1_Updating(object sender, SqlDataSourceCommandEventArgs e)
            {
                //int Gender = Gender(GridView1.DropDownList3.Text);
                DropDownList G1 = (DropDownList)this.GridView1.FindControl("DropDownList3");
                DropDownList R1 = (DropDownList)this.GridView1.FindControl("DropDownList2");
                DropDownList M1 = (DropDownList)this.GridView1.FindControl("DropDownList1");
                int gender1 = Gender(G1.SelectedValue);
                int religion1 = Religion(R1.SelectedValue);
                int marital1 = Marital(M1.SelectedValue);
                try
                {
                    e.Command.Parameters["@Gender"].Value = gender1;
                    e.Command.Parameters["@ReligionId"].Value = religion1;
                    e.Command.Parameters["@MaritalID"].Value = marital1;
     
                }
                catch
                {
     
                }
            }

    and this is the error message:

    how can I do edit in my grid correctly?!

    Monday, January 14, 2019 8:09 AM

Answers

  • User283571144 posted

    Hi dorsa2,

    According to your description and codes, this error indicates  you did not get the right DropDownList  from GridView1,since there are many dropdownlists in this gridview..

    I suggest you could get edit row index firstly in the GridView’s OnRowUpdating event, then you could get the right  DropDownList selected value according to this index.

    More details, you could refer to below codes:

    ASPX:

    Notice: Since I don't have your datatable, I modify some data type.

            <div>
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowUpdating="GridView1_RowUpdating" DataKeyNames="Id" DataSourceID="SqlDataSource1">
                    <Columns>
                         <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
                        <asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True" SortExpression="Id" />
                        <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
                        <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
                        <asp:BoundField DataField="BirthDate" HeaderText="BirthDate" SortExpression="BirthDate" />
                        <asp:BoundField DataField="BirthPlace" HeaderText="BirthPlace" SortExpression="BirthPlace" />
                      
                         <asp:TemplateField HeaderText="Gender" SortExpression="Gender">
                        <EditItemTemplate>
                            <asp:DropDownList ID="DropDownList1" runat="server">
                                <asp:ListItem Value="0">زن</asp:ListItem>
                                <asp:ListItem Value="1">مرد</asp:ListItem>
                            </asp:DropDownList>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Bind("Gender") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                         <asp:TemplateField HeaderText="ReligionId" SortExpression="ReligionId">
                        <EditItemTemplate>
                            <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="Religion" DataTextField="title" DataValueField="title">
                            </asp:DropDownList>
                            <asp:SqlDataSource ID="Religion" runat="server"  ConnectionString="<%$ ConnectionStrings:data11ConnectionString %>" 
                                SelectCommand="SELECT [title] FROM [IMS_Religion]"></asp:SqlDataSource>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label2" runat="server" Text='<%# Bind("ReligionId") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
    
                        <asp:TemplateField HeaderText="MaritalId" SortExpression="MaritalId">
                        <EditItemTemplate>
                            <asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="Marital" DataTextField="Status" DataValueField="Status">
                            </asp:DropDownList>
                            <asp:SqlDataSource ID="Marital" runat="server"  ConnectionString="<%$ ConnectionStrings:data11ConnectionString %>"
                                SelectCommand="SELECT [Status] FROM [IMS_Marital]"></asp:SqlDataSource>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label3" runat="server" Text='<%# Bind("MaritalId") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>                  
                    </Columns>
                </asp:GridView>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:data11ConnectionString %>"
                    SelectCommand="SELECT * FROM [IMS_Person]"
                    DeleteCommand="DELETE FROM [IMS_Person] WHERE [Id] = @Id" 
                    InsertCommand="INSERT INTO [IMS_Person] ([Id], [FirstName], [LastName], [BirthDate], [BirthPlace], [Gender], [ReligionId], [Religion], [MaritalId], [Marital]) VALUES (@Id, @FirstName, @LastName, @BirthDate, @BirthPlace, @Gender, @ReligionId, @Religion, @MaritalId, @Marital)" 
                    UpdateCommand="UPDATE [IMS_Person] SET [FirstName] = @FirstName, [LastName] = @LastName, [BirthDate] = @BirthDate, [BirthPlace] = @BirthPlace, [Gender] = @Gender, [ReligionId] = @ReligionId, [Religion] = @Religion, [MaritalId] = @MaritalId, [Marital] = @Marital WHERE [Id] = @Id"
                     OnUpdating="SqlDataSource1_Updating">
                    <DeleteParameters>
                        <asp:Parameter Name="Id" Type="Int32" />
                    </DeleteParameters>
                    <InsertParameters>
                        <asp:Parameter Name="Id" Type="Int32" />
                        <asp:Parameter Name="FirstName" Type="String" />
                        <asp:Parameter Name="LastName" Type="String" />
                        <asp:Parameter Name="BirthDate" Type="String" />
                        <asp:Parameter Name="BirthPlace" Type="String" />
                        <asp:Parameter Name="Gender" Type="String" />
                        <asp:Parameter Name="ReligionId" Type="String" />
                        <asp:Parameter Name="Religion" Type="String" />
                        <asp:Parameter Name="MaritalId" Type="Int32" />
                        <asp:Parameter Name="Marital" Type="String" />
                    </InsertParameters>
                    <UpdateParameters>
                        <asp:Parameter Name="FirstName" Type="String" />
                        <asp:Parameter Name="LastName" Type="String" />
                        <asp:Parameter Name="BirthDate" Type="String" />
                        <asp:Parameter Name="BirthPlace" Type="String" />
                        <asp:Parameter Name="Gender" Type="String" />
                        <asp:Parameter Name="ReligionId" Type="String" />
                        <asp:Parameter Name="Religion" Type="String" />
                        <asp:Parameter Name="MaritalId" Type="Int32" />
                        <asp:Parameter Name="Marital" Type="String" />
                        <asp:Parameter Name="Id" Type="Int32" />
                    </UpdateParameters>
                </asp:SqlDataSource>
            </div>
    
    
    

    Code-behind:

    protected void SqlDataSource1_Updating(object sender, SqlDataSourceCommandEventArgs e)
            {
                //int Gender = Gender(GridView1.DropDownList3.Text);
                int i  = (int)ViewState["rowindex"];
                GridViewRow row = (GridViewRow)GridView1.Rows[i];
                DropDownList G1 = (DropDownList)row.FindControl("DropDownList3");           
                DropDownList R1 = (DropDownList)row.FindControl("DropDownList2");
                DropDownList M1 = (DropDownList)row.FindControl("DropDownList1");
          
                // int gender1 = Gender(G1.SelectedValue);
               // int religion1 = Religion(R1.SelectedValue);
                //int marital1 = Marital(M1.SelectedValue);
                //try
                //{
                //    e.Command.Parameters["@Gender"].Value = gender1;
                //    e.Command.Parameters["@ReligionId"].Value = religion1;
                //    e.Command.Parameters["@MaritalID"].Value = marital1;
    
                //}
                //catch
                //{
    
                //}
            }
        
            protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
            {
                ViewState["rowindex"] = e.RowIndex;
            }
    
    

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 16, 2019 3:15 AM

All replies

  • User-1716253493 posted

    Use GridView1.Rows(idx).FindControls()

    Idx is gridview1.EditIndex

    Tuesday, January 15, 2019 10:52 AM
  • User283571144 posted

    Hi dorsa2,

    According to your description and codes, this error indicates  you did not get the right DropDownList  from GridView1,since there are many dropdownlists in this gridview..

    I suggest you could get edit row index firstly in the GridView’s OnRowUpdating event, then you could get the right  DropDownList selected value according to this index.

    More details, you could refer to below codes:

    ASPX:

    Notice: Since I don't have your datatable, I modify some data type.

            <div>
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowUpdating="GridView1_RowUpdating" DataKeyNames="Id" DataSourceID="SqlDataSource1">
                    <Columns>
                         <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
                        <asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True" SortExpression="Id" />
                        <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
                        <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
                        <asp:BoundField DataField="BirthDate" HeaderText="BirthDate" SortExpression="BirthDate" />
                        <asp:BoundField DataField="BirthPlace" HeaderText="BirthPlace" SortExpression="BirthPlace" />
                      
                         <asp:TemplateField HeaderText="Gender" SortExpression="Gender">
                        <EditItemTemplate>
                            <asp:DropDownList ID="DropDownList1" runat="server">
                                <asp:ListItem Value="0">زن</asp:ListItem>
                                <asp:ListItem Value="1">مرد</asp:ListItem>
                            </asp:DropDownList>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Bind("Gender") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                         <asp:TemplateField HeaderText="ReligionId" SortExpression="ReligionId">
                        <EditItemTemplate>
                            <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="Religion" DataTextField="title" DataValueField="title">
                            </asp:DropDownList>
                            <asp:SqlDataSource ID="Religion" runat="server"  ConnectionString="<%$ ConnectionStrings:data11ConnectionString %>" 
                                SelectCommand="SELECT [title] FROM [IMS_Religion]"></asp:SqlDataSource>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label2" runat="server" Text='<%# Bind("ReligionId") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
    
                        <asp:TemplateField HeaderText="MaritalId" SortExpression="MaritalId">
                        <EditItemTemplate>
                            <asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="Marital" DataTextField="Status" DataValueField="Status">
                            </asp:DropDownList>
                            <asp:SqlDataSource ID="Marital" runat="server"  ConnectionString="<%$ ConnectionStrings:data11ConnectionString %>"
                                SelectCommand="SELECT [Status] FROM [IMS_Marital]"></asp:SqlDataSource>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label3" runat="server" Text='<%# Bind("MaritalId") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>                  
                    </Columns>
                </asp:GridView>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:data11ConnectionString %>"
                    SelectCommand="SELECT * FROM [IMS_Person]"
                    DeleteCommand="DELETE FROM [IMS_Person] WHERE [Id] = @Id" 
                    InsertCommand="INSERT INTO [IMS_Person] ([Id], [FirstName], [LastName], [BirthDate], [BirthPlace], [Gender], [ReligionId], [Religion], [MaritalId], [Marital]) VALUES (@Id, @FirstName, @LastName, @BirthDate, @BirthPlace, @Gender, @ReligionId, @Religion, @MaritalId, @Marital)" 
                    UpdateCommand="UPDATE [IMS_Person] SET [FirstName] = @FirstName, [LastName] = @LastName, [BirthDate] = @BirthDate, [BirthPlace] = @BirthPlace, [Gender] = @Gender, [ReligionId] = @ReligionId, [Religion] = @Religion, [MaritalId] = @MaritalId, [Marital] = @Marital WHERE [Id] = @Id"
                     OnUpdating="SqlDataSource1_Updating">
                    <DeleteParameters>
                        <asp:Parameter Name="Id" Type="Int32" />
                    </DeleteParameters>
                    <InsertParameters>
                        <asp:Parameter Name="Id" Type="Int32" />
                        <asp:Parameter Name="FirstName" Type="String" />
                        <asp:Parameter Name="LastName" Type="String" />
                        <asp:Parameter Name="BirthDate" Type="String" />
                        <asp:Parameter Name="BirthPlace" Type="String" />
                        <asp:Parameter Name="Gender" Type="String" />
                        <asp:Parameter Name="ReligionId" Type="String" />
                        <asp:Parameter Name="Religion" Type="String" />
                        <asp:Parameter Name="MaritalId" Type="Int32" />
                        <asp:Parameter Name="Marital" Type="String" />
                    </InsertParameters>
                    <UpdateParameters>
                        <asp:Parameter Name="FirstName" Type="String" />
                        <asp:Parameter Name="LastName" Type="String" />
                        <asp:Parameter Name="BirthDate" Type="String" />
                        <asp:Parameter Name="BirthPlace" Type="String" />
                        <asp:Parameter Name="Gender" Type="String" />
                        <asp:Parameter Name="ReligionId" Type="String" />
                        <asp:Parameter Name="Religion" Type="String" />
                        <asp:Parameter Name="MaritalId" Type="Int32" />
                        <asp:Parameter Name="Marital" Type="String" />
                        <asp:Parameter Name="Id" Type="Int32" />
                    </UpdateParameters>
                </asp:SqlDataSource>
            </div>
    
    
    

    Code-behind:

    protected void SqlDataSource1_Updating(object sender, SqlDataSourceCommandEventArgs e)
            {
                //int Gender = Gender(GridView1.DropDownList3.Text);
                int i  = (int)ViewState["rowindex"];
                GridViewRow row = (GridViewRow)GridView1.Rows[i];
                DropDownList G1 = (DropDownList)row.FindControl("DropDownList3");           
                DropDownList R1 = (DropDownList)row.FindControl("DropDownList2");
                DropDownList M1 = (DropDownList)row.FindControl("DropDownList1");
          
                // int gender1 = Gender(G1.SelectedValue);
               // int religion1 = Religion(R1.SelectedValue);
                //int marital1 = Marital(M1.SelectedValue);
                //try
                //{
                //    e.Command.Parameters["@Gender"].Value = gender1;
                //    e.Command.Parameters["@ReligionId"].Value = religion1;
                //    e.Command.Parameters["@MaritalID"].Value = marital1;
    
                //}
                //catch
                //{
    
                //}
            }
        
            protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
            {
                ViewState["rowindex"] = e.RowIndex;
            }
    
    

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 16, 2019 3:15 AM