locked
How to edit a gridview row RRS feed

  • Question

  • User789306677 posted

    I have working with a gridview that the customer wants to edit on the row level.  I have a couple of questions:

    1) There is a dropdownlist and two text boxes. that populate the values in that row.  Do I have to use template fields for this instead bound fields?

    2) On the dropdown I want to set the selected value to the original item.  I assumed I need to do this in a code behind. Would this be rowCommand for the gridview or row_edting. I have tried on rowcommand and when I try to find the control using the id, it doesn't find it.  

    I will reply with any source code needed.

    Thanks in advance

    Wednesday, February 22, 2017 9:03 PM

Answers

  • User2103319870 posted

    There is a dropdownlist and two text boxes. that populate the values in that row.  Do I have to use template fields for this instead bound fields?

    Yes you have to use TemplateField column and use the EditItemTemplate for dropdownlist

    On the dropdown I want to set the selected value to the original item.  I assumed I need to do this in a code behind.

    You can do it in html mark up itself like below

     <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2" DataTextField="AddressID" DataValueField="AddressID" SelectedValue='<%# Bind("AddressID", "{0}") %>'></asp:DropDownList>

    Complete Code

     <asp:GridView ID="GridView1" runat="server" CellPadding="4" Font-Size="Small" CauseVaidation="false" AutoGenerateColumns="False" AllowSorting="True"
                    DataSourceID="SqlDataSource1" AllowPaging="True" GridLines="Horizontal"
                    DataKeyNames="AddressID" BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" OnRowEditing="GridView1_RowEditing">
                    <Columns>
                        <asp:TemplateField HeaderText="Test" SortExpression="Coach Name">
                            <ItemTemplate>
                                <asp:Label ID="FirstNameLabel" runat="Server" Text='<%#Eval("AddressID") %>' />
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2" DataTextField="AddressID" DataValueField="AddressID" SelectedValue='<%# Bind("AddressID", "{0}") %>'></asp:DropDownList>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="AddressLine1" HeaderText="AddressLine1"></asp:BoundField>
                        <asp:CommandField ShowEditButton="true" />
                    </Columns>
                </asp:GridView>
    
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:AdventureWorksConnectionString %>"
                    SelectCommand="SELECT top 10 [AddressID], [AddressLine1], [AddressLine2], [City] FROM Person.[Address]"></asp:SqlDataSource>
                <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:AdventureWorksConnectionString %>"
                    SelectCommand="SELECT top 10 [AddressID], [AddressLine1], [AddressLine2], [City] FROM Person.[Address]"></asp:SqlDataSource>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 22, 2017 9:50 PM

All replies

  • User2103319870 posted

    There is a dropdownlist and two text boxes. that populate the values in that row.  Do I have to use template fields for this instead bound fields?

    Yes you have to use TemplateField column and use the EditItemTemplate for dropdownlist

    On the dropdown I want to set the selected value to the original item.  I assumed I need to do this in a code behind.

    You can do it in html mark up itself like below

     <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2" DataTextField="AddressID" DataValueField="AddressID" SelectedValue='<%# Bind("AddressID", "{0}") %>'></asp:DropDownList>

    Complete Code

     <asp:GridView ID="GridView1" runat="server" CellPadding="4" Font-Size="Small" CauseVaidation="false" AutoGenerateColumns="False" AllowSorting="True"
                    DataSourceID="SqlDataSource1" AllowPaging="True" GridLines="Horizontal"
                    DataKeyNames="AddressID" BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" OnRowEditing="GridView1_RowEditing">
                    <Columns>
                        <asp:TemplateField HeaderText="Test" SortExpression="Coach Name">
                            <ItemTemplate>
                                <asp:Label ID="FirstNameLabel" runat="Server" Text='<%#Eval("AddressID") %>' />
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2" DataTextField="AddressID" DataValueField="AddressID" SelectedValue='<%# Bind("AddressID", "{0}") %>'></asp:DropDownList>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="AddressLine1" HeaderText="AddressLine1"></asp:BoundField>
                        <asp:CommandField ShowEditButton="true" />
                    </Columns>
                </asp:GridView>
    
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:AdventureWorksConnectionString %>"
                    SelectCommand="SELECT top 10 [AddressID], [AddressLine1], [AddressLine2], [City] FROM Person.[Address]"></asp:SqlDataSource>
                <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:AdventureWorksConnectionString %>"
                    SelectCommand="SELECT top 10 [AddressID], [AddressLine1], [AddressLine2], [City] FROM Person.[Address]"></asp:SqlDataSource>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 22, 2017 9:50 PM
  • User-1716253493 posted

    Binding

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if(e.Row.RowType==DataControlRowType.DataRow)
            {
                if(e.Row.RowState==DataControlRowState.Edit)
                {
                    string country = DataBinder.Eval(e.Row.DataItem, "country").ToString();
                    DropDownList ddlcountry = (DropDownList)e.Row.FindControl("ddlcountry");
    //populate ddlcountry items here if(ddlcountry.Items.FindByValue(country)!=null) { ddlcountry.Items.FindByValue(country).Selected = true; } } } }

    Saving

    DataKeyNames

                <asp:GridView ID="GridView1" runat="server" DataKeyNames="ID" 
                     OnRowDataBound="GridView1_RowDataBound" OnRowUpdating="GridView1_RowUpdating">
                </asp:GridView>
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int editidx = GridView1.EditIndex;
            DropDownList ddlcountry = (DropDownList)GridView1.Rows[editidx].FindControl("ddlcountry");
            int id = Convert.ToInt32(GridView1.DataKeys[editidx].Values[0]);
            //saving
    
        }



    Thursday, February 23, 2017 12:56 AM