Objectmix
Tags Register Mark Forums Read

Editing gridview : DOTNET

This is a discussion on Editing gridview within the DOTNET forums in Framework and Interface Programming category; 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 ...


Object Mix > Framework and Interface Programming > DOTNET > Editing gridview

DOTNET Discussion forums related to Microsoft Dot net technologies, CSharp and other related items

Reply

 

LinkBack Thread Tools
  #1  
Old 10-30-2007, 11:04 AM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Editing gridview

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 ***
  #2  
Old 10-30-2007, 11:28 AM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: Editing gridview

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" <mike.parr@gmail.com> wrote in message
news:uBzNQ6wGIHA.4880@TK2MSFTNGP03.phx.gbl...
>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 ***



  #3  
Old 10-30-2007, 11:48 AM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: Editing gridview

Eliyahu,

Do you have a code sample?

Thanks,

Mike


*** Sent via Developersdex http://www.developersdex.com ***
  #4  
Old 10-31-2007, 03:55 AM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: Editing gridview

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" <mike.parr@gmail.com> wrote in message
news:uBzNQ6wGIHA.4880@TK2MSFTNGP03.phx.gbl...
>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 ***


  #5  
Old 10-31-2007, 05:32 AM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: Editing gridview


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

Thread Tools


Similar Threads

Thread Thread Starter Forum Replies Last Post
GridView - two clicks needed to enter in-place editing usenet DOTNET 9 09-16-2008 04:34 AM
GridView -enable editing usenet DOTNET 5 10-30-2007 05:47 AM
Editing Single Cell on GridView usenet DOTNET 5 09-14-2007 07:25 AM
GridView Control - Disable/Enable Row Editing usenet DOTNET 1 07-30-2007 09:41 AM
editing in gridview usenet DOTNET 2 07-17-2007 06:02 AM


All times are GMT -5. The time now is 09:01 AM.

Managed by Infnx Pvt Ltd.