Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > GridView as EditItem Template

Reply
Thread Tools

GridView as EditItem Template

 
 
Dariusz Tomon
Guest
Posts: n/a
 
      04-04-2007
Hello,

I have GridView in EditItem Template of DetailsView (in normal - read-only
mode there is a label). When I'm in
edit mode I can select one item from GridView. The main problem is that as
I can see in DataBidings property of the GridView I deliver SelectedIndex
instead of SelectedValue. Therefore I gives SelectedIndex value to update in
that specific field and NOT SelectedValue (it is ID - foreign key of parent
table)

How can I resolve the problem so that I pass value not an index?
I would appreciate for a code snippet if requred for the problem solution


Best Regards

Darek T.



 
Reply With Quote
 
 
 
 
=?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=
Guest
Posts: n/a
 
      04-04-2007
Czolem Darek,

Could you provide the code to clarify what exactly you're trying to achieve.
--
Milosz


"Dariusz Tomon" wrote:

> Hello,
>
> I have GridView in EditItem Template of DetailsView (in normal - read-only
> mode there is a label). When I'm in
> edit mode I can select one item from GridView. The main problem is that as
> I can see in DataBidings property of the GridView I deliver SelectedIndex
> instead of SelectedValue. Therefore I gives SelectedIndex value to update in
> that specific field and NOT SelectedValue (it is ID - foreign key of parent
> table)
>
> How can I resolve the problem so that I pass value not an index?
> I would appreciate for a code snippet if requred for the problem solution
>
>
> Best Regards
>
> Darek T.
>
>
>
>

 
Reply With Quote
 
 
 
 
Dariusz Tomon
Guest
Posts: n/a
 
      04-05-2007
Czolem,

.......
<EditItemTemplate>

<asp:GridView ID="gv_DeviceModel" runat="server"
DataSourceID="Sql_DeviceModel" SelectedIndex='<%# Bind("IDDeviceModel") %>'>

<Columns>

<asp:CommandField ShowSelectButton="True" />

</Columns>

</asp:GridView>

</EditItemTemplate>

.......

So as you can see I have GridView as EditItemTemplate but there is
SelectedIndex='<%# Bind("IDDeviceModel") %>' the value of ID in my table
(IDDeviceModel) is connected with SelectedIndex (and should be connected
with SelectedValue rather).

So when I choose an item and make update I get other item unfortunatelly. I
think that in the scenario when my ID were of GUID type or something like
that I would get error but in my case idex type ad ID type are the same so
there is no error but annoing issue.

I hope that now it's clear

Pozdrawiam



Darek


"Milosz Skalecki [MCAD]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Czolem Darek,
>
> Could you provide the code to clarify what exactly you're trying to
> achieve.
> --
> Milosz
>
>
> "Dariusz Tomon" wrote:
>
>> Hello,
>>
>> I have GridView in EditItem Template of DetailsView (in normal -
>> read-only
>> mode there is a label). When I'm in
>> edit mode I can select one item from GridView. The main problem is that
>> as
>> I can see in DataBidings property of the GridView I deliver SelectedIndex
>> instead of SelectedValue. Therefore I gives SelectedIndex value to update
>> in
>> that specific field and NOT SelectedValue (it is ID - foreign key of
>> parent
>> table)
>>
>> How can I resolve the problem so that I pass value not an index?
>> I would appreciate for a code snippet if requred for the problem solution
>>
>>
>> Best Regards
>>
>> Darek T.
>>
>>
>>
>>



 
Reply With Quote
 
=?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=
Guest
Posts: n/a
 
      04-05-2007
Czolgiem,

OK, i see what you're trying to do. The resolution is pretty straitforward -
you need to find corresponding row index when binding internal grid view,
therefore you need to bind grid's view rowdatabound event:

private bool found = false;
protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataGridItem item = e.Row;

if (found)
return;

if (item.ItemType == ListItemType.Item ||
item.ItemType == ListItemType.AlternatingItem)
{
// i assume you bind the data to a datatable or a dataset
DataRow row = ((DataRowView) (item.DataItem)).Row;

// note i assume you use datakeynames property of the DetailsView1
int id = (int) row["IDDeviceModel"];
if (id == (int) DetailsView1.SelectedValue)
{
gridView.SelectedIndex = item.ItemIndex;
found = true;
}
}
}


Alternatively, another approach would be to find the corresponding row
manually (of course it can only be applied if you know the datasource you
bind to gridview control, i.e data bound to details view is hierarchical and
you bind children to the grid view):

protected int FindIndex(DataRow rows, int IDDeviceModel)
{

for (int i = 0; i < rows.Count; i++)
{
DataRow row = rows[i];

// i assume
if ((int) row["IDDeviceModel"] == IDDeviceModel)
return i;
}

return -1;

}

and aspx code:
<asp:GridView ID="gv_DeviceModel" runat="server"
DataSourceID="Sql_DeviceModel" SelectedIndex='<%#
FindIndex(((DataRow)Container.DataItem).GetChildRo ws(),
(int)Eval("IDDeviceModel")) %>'>

Hope this helps
--
Milosz


"Dariusz Tomon" wrote:

> Czolem,
>
> .......
> <EditItemTemplate>
>
> <asp:GridView ID="gv_DeviceModel" runat="server"
> DataSourceID="Sql_DeviceModel" SelectedIndex='<%# Bind("IDDeviceModel") %>'>
>
> <Columns>
>
> <asp:CommandField ShowSelectButton="True" />
>
> </Columns>
>
> </asp:GridView>
>
> </EditItemTemplate>
>
> .......
>
> So as you can see I have GridView as EditItemTemplate but there is
> SelectedIndex='<%# Bind("IDDeviceModel") %>' the value of ID in my table
> (IDDeviceModel) is connected with SelectedIndex (and should be connected
> with SelectedValue rather).
>
> So when I choose an item and make update I get other item unfortunatelly. I
> think that in the scenario when my ID were of GUID type or something like
> that I would get error but in my case idex type ad ID type are the same so
> there is no error but annoing issue.
>
> I hope that now it's clear
>
> Pozdrawiam
>
>
>
> Darek
>
>
> "Milosz Skalecki [MCAD]" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > Czolem Darek,
> >
> > Could you provide the code to clarify what exactly you're trying to
> > achieve.
> > --
> > Milosz
> >
> >
> > "Dariusz Tomon" wrote:
> >
> >> Hello,
> >>
> >> I have GridView in EditItem Template of DetailsView (in normal -
> >> read-only
> >> mode there is a label). When I'm in
> >> edit mode I can select one item from GridView. The main problem is that
> >> as
> >> I can see in DataBidings property of the GridView I deliver SelectedIndex
> >> instead of SelectedValue. Therefore I gives SelectedIndex value to update
> >> in
> >> that specific field and NOT SelectedValue (it is ID - foreign key of
> >> parent
> >> table)
> >>
> >> How can I resolve the problem so that I pass value not an index?
> >> I would appreciate for a code snippet if requred for the problem solution
> >>
> >>
> >> Best Regards
> >>
> >> Darek T.
> >>
> >>
> >>
> >>

>
>
>

 
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
link textboxes in gridview EditItem template using javascript Jason ASP .Net 1 07-12-2010 08:41 AM
Events in DataList EditItem Template won't fire Crazy Cat ASP .Net 3 10-14-2008 01:17 PM
Bug: Listview edititem: First item in list has a null edititem James Cooke ASP .Net Web Controls 0 09-11-2008 07:25 PM
Re-use the EditItem template JP.Gantlin ASP .Net 1 10-04-2006 08:36 AM
EditItem Template =?Utf-8?B?SmltIEhlYXZleQ==?= ASP .Net 1 02-18-2005 05:05 PM



Advertisments