GridView as EditItem Template

D

Dariusz Tomon

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.
 
G

Guest

Czolem Darek,

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

Dariusz Tomon

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
 
G

Guest

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 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).GetChildRows(),
(int)Eval("IDDeviceModel")) %>'>

Hope this helps
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top