Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Editing gridview

Reply
Thread Tools

Editing gridview

 
 
Mike P
Guest
Posts: n/a
 
      10-30-2007
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>
<aspropDownList 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 ***
 
Reply With Quote
 
 
 
 
Eliyahu Goldin
Guest
Posts: n/a
 
      10-30-2007
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>
> <aspropDownList 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 ***



 
Reply With Quote
 
 
 
 
Mike P
Guest
Posts: n/a
 
      10-30-2007
Eliyahu,

Do you have a code sample?

Thanks,

Mike


*** Sent via Developersdex http://www.developersdex.com ***
 
Reply With Quote
 
Dave Sussman
Guest
Posts: n/a
 
      10-31-2007
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:

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

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>
> <aspropDownList 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 ***


 
Reply With Quote
 
Mansi Shah
Guest
Posts: n/a
 
      10-31-2007

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[i].value=="xyz")
{
sponsers.selectedvalue=sponsers.items[i].value;
break;
}
}
}
}

Hope this will help you.

Regards,
Mansi Shah.

*** Sent via Developersdex http://www.developersdex.com ***
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to dynamically enable/disable row editing in GridView control (ASP.NET 2.0) ? misiek ASP .Net 7 04-08-2011 06:42 AM
Are you looking for high quality photo editing and video editing? gwanglu@gmail.com C Programming 0 07-19-2006 12:32 PM
GridView Hierarchical View - Gridview in Gridview =?Utf-8?B?bWdvbnphbGVzMw==?= ASP .Net 1 05-09-2006 06:48 PM
Gridview Newvalues are empy after editing =?Utf-8?B?TWljaGFlbA==?= ASP .Net 0 04-13-2006 07:04 PM
Editing with GridView control Fred K. Augustine, Jr ASP .Net 0 11-21-2005 07:07 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57