469,927 Members | 1,768 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,927 developers. It's quick & easy.

GridView as EditItem Template

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.

Apr 4 '07 #1
3 3510
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.

Apr 4 '07 #2
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]" <mi*****@DONTLIKESPAMwp.plwrote in message
news:62**********************************@microsof t.com...
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.


Apr 5 '07 #3
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]" <mi*****@DONTLIKESPAMwp.plwrote in message
news:62**********************************@microsof t.com...
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.



Apr 5 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by aarif.shah | last post: by
2 posts views Thread by GCeaser | last post: by
reply views Thread by Dariusz Tomon | last post: by
3 posts views Thread by Crazy Cat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.