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_RowDat aBound(object sender, GridViewRowEven tArgs e)
{
DataGridItem item = e.Row;
if (found)
return;
if (item.ItemType == ListItemType.It em ||
item.ItemType == ListItemType.Al ternatingItem)
{
// 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["IDDeviceMo del"];
if (id == (int) DetailsView1.Se lectedValue)
{
gridView.Select edIndex = 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(DataR ow rows, int IDDeviceModel)
{
for (int i = 0; i < rows.Count; i++)
{
DataRow row = rows[i];
// i assume
if ((int) row["IDDeviceMo del"] == IDDeviceModel)
return i;
}
return -1;
}
and aspx code:
<asp:GridView ID="gv_DeviceMo del" runat="server"
DataSourceID="S ql_DeviceModel" SelectedIndex=' <%#
FindIndex(((Dat aRow)Container. DataItem).GetCh ildRows(),
(int)Eval("IDDe viceModel")) %>'>
Hope this helps
--
Milosz
"Dariusz Tomon" wrote:
Czolem,
.......
<EditItemTempla te>
<asp:GridView ID="gv_DeviceMo del" runat="server"
DataSourceID="S ql_DeviceModel" SelectedIndex=' <%# Bind("IDDeviceM odel") %>'>
<Columns>
<asp:CommandFie ld ShowSelectButto n="True" />
</Columns>
</asp:GridView>
</EditItemTemplat e>
.......
So as you can see I have GridView as EditItemTemplat e but there is
SelectedIndex=' <%# Bind("IDDeviceM odel") %>' 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*****@DONTLI KESPAMwp.plwrot e in message
news:62******** *************** ***********@mic rosoft.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.