GridView as EditItem Template

Discussion in 'ASP .Net' started by Dariusz Tomon, Apr 4, 2007.

  1. 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.
    Dariusz Tomon, Apr 4, 2007
    #1
    1. Advertising

  2. 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.
    >
    >
    >
    >
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=, Apr 4, 2007
    #2
    1. Advertising

  3. 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]" <> wrote in message
    news:...
    > 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.
    >>
    >>
    >>
    >>
    Dariusz Tomon, Apr 5, 2007
    #3
  4. 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
    --
    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]" <> wrote in message
    > news:...
    > > 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.
    > >>
    > >>
    > >>
    > >>

    >
    >
    >
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=, Apr 5, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SmltIEhlYXZleQ==?=

    EditItem Template

    =?Utf-8?B?SmltIEhlYXZleQ==?=, Feb 18, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    489
    =?Utf-8?B?QmlsbCBCb3Jn?=
    Feb 18, 2005
  2. JP.Gantlin

    Re-use the EditItem template

    JP.Gantlin, Oct 4, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    335
    Eliyahu Goldin
    Oct 4, 2006
  3. Crazy Cat
    Replies:
    3
    Views:
    366
    Crazy Cat
    Oct 14, 2008
  4. Jason
    Replies:
    1
    Views:
    1,996
    Shengqing Yang [MSFT]
    Jul 12, 2010
  5. James Cooke
    Replies:
    0
    Views:
    220
    James Cooke
    Sep 11, 2008
Loading...

Share This Page