Editing gridview

Discussion in 'ASP .Net' started by Mike P, Oct 30, 2007.

  1. Mike P

    Mike P Guest

    I am creating a gridview that can be edited by the user, and one of the
    fields to be edited I want to give the user a drop down which is
    populated by a db field. This works fine when the current record being
    edited already has a value for the dropdown, but there are cases where
    the record might not have a value for the dropdown field, which means
    that the SelectedValue property causes the app to crash as there is
    nothing in the DB table to populate this record.

    Here is my code for the field :

    <asp:TemplateField HeaderText="Sponsor" SortExpression="Sponsor">
    <ItemTemplate>
    <asp:Label ID="lblSponsor" Text='<%#
    Eval("Sponsor") %>' runat="server"></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:DropDownList ID="ddlSponsors"
    runat="server" DataSourceID="SqlDataSource2"
    DataTextField="Sponsor"
    DataValueField="Sponsor" SelectedValue="Sponsor" />
    </EditItemTemplate>
    </asp:TemplateField>

    Can anybody help?



    *** Sent via Developersdex http://www.developersdex.com ***
    Mike P, Oct 30, 2007
    #1
    1. Advertising

  2. You can handle RowEditing event to select ddl items only if they are there.

    --
    Eliyahu Goldin,
    Software Developer
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin
    http://usableasp.net


    "Mike P" <> wrote in message
    news:...
    >I am creating a gridview that can be edited by the user, and one of the
    > fields to be edited I want to give the user a drop down which is
    > populated by a db field. This works fine when the current record being
    > edited already has a value for the dropdown, but there are cases where
    > the record might not have a value for the dropdown field, which means
    > that the SelectedValue property causes the app to crash as there is
    > nothing in the DB table to populate this record.
    >
    > Here is my code for the field :
    >
    > <asp:TemplateField HeaderText="Sponsor" SortExpression="Sponsor">
    > <ItemTemplate>
    > <asp:Label ID="lblSponsor" Text='<%#
    > Eval("Sponsor") %>' runat="server"></asp:Label>
    > </ItemTemplate>
    > <EditItemTemplate>
    > <asp:DropDownList ID="ddlSponsors"
    > runat="server" DataSourceID="SqlDataSource2"
    > DataTextField="Sponsor"
    > DataValueField="Sponsor" SelectedValue="Sponsor" />
    > </EditItemTemplate>
    > </asp:TemplateField>
    >
    > Can anybody help?
    >
    >
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    Eliyahu Goldin, Oct 30, 2007
    #2
    1. Advertising

  3. Mike P

    Mike P Guest

    Mike P, Oct 30, 2007
    #3
  4. Mike P

    Dave Sussman Guest

    One way of achieving this is to have "not selected" option, perhaps with a
    blank value. The DropDownList allows you to append bound items, so you could
    have:

    <asp:DropDownList ID="ddlSponsors" runat="server"
    DataSourceID="SqlDataSource2"
    AppendDtaBoundItems="true"
    DataTextField="Sponsor" DataValueField="Sponsor"
    SelectedValue="Sponsor">
    <asp:ListItem Value="" Text="-- not selected --" />
    </asp:DropDownList>

    Dave


    "Mike P" <> wrote in message
    news:...
    >I am creating a gridview that can be edited by the user, and one of the
    > fields to be edited I want to give the user a drop down which is
    > populated by a db field. This works fine when the current record being
    > edited already has a value for the dropdown, but there are cases where
    > the record might not have a value for the dropdown field, which means
    > that the SelectedValue property causes the app to crash as there is
    > nothing in the DB table to populate this record.
    >
    > Here is my code for the field :
    >
    > <asp:TemplateField HeaderText="Sponsor" SortExpression="Sponsor">
    > <ItemTemplate>
    > <asp:Label ID="lblSponsor" Text='<%#
    > Eval("Sponsor") %>' runat="server"></asp:Label>
    > </ItemTemplate>
    > <EditItemTemplate>
    > <asp:DropDownList ID="ddlSponsors"
    > runat="server" DataSourceID="SqlDataSource2"
    > DataTextField="Sponsor"
    > DataValueField="Sponsor" SelectedValue="Sponsor" />
    > </EditItemTemplate>
    > </asp:TemplateField>
    >
    > Can anybody help?
    >
    >
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    Dave Sussman, Oct 31, 2007
    #4
  5. Mike P

    Mansi Shah Guest

    Hi,

    Here is a code sample for gridview_rowediting()

    protected void gvAmazoneOrders_RowEditing(object sender,
    GridViewEditEventArgs e)
    {
    int index=e.neweditindex;
    Dropdownlist sponsers=(DropDownList)
    Gridview1.rows[index].findcontrol("ddlSponsors");
    //Here check if "sponsers" has the value you want or not.
    //like,
    for(int i=0;i<sponsers.items.count;i++)
    {
    if(sponsers.items.value=="xyz")
    {
    sponsers.selectedvalue=sponsers.items.value;
    break;
    }
    }
    }
    }

    Hope this will help you.

    Regards,
    Mansi Shah.

    *** Sent via Developersdex http://www.developersdex.com ***
    Mansi Shah, Oct 31, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fred K. Augustine, Jr

    Editing with GridView control

    Fred K. Augustine, Jr, Nov 21, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    920
    Fred K. Augustine, Jr
    Nov 21, 2005
  2. misiek
    Replies:
    7
    Views:
    50,152
    Sri123
    Apr 8, 2011
  3. =?Utf-8?B?TWljaGFlbA==?=

    Gridview Newvalues are empy after editing

    =?Utf-8?B?TWljaGFlbA==?=, Apr 13, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    1,814
    =?Utf-8?B?TWljaGFlbA==?=
    Apr 13, 2006
  4. =?Utf-8?B?bWdvbnphbGVzMw==?=

    GridView Hierarchical View - Gridview in Gridview

    =?Utf-8?B?bWdvbnphbGVzMw==?=, May 9, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    17,944
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=
    May 9, 2006
  5. Replies:
    0
    Views:
    572
Loading...

Share This Page