C# - ASP - Gridview

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • WayDownUnder

    C# - ASP - Gridview

    Hello,
    I have a gridview control bound to a objectdatasourc e.
    The primary key column is on the grid but made visible = false.
    Because the column is invisible I cant get to the cell value when the user
    presses
    the edit button on the grid.
    Code

    protected void grdBedpDetails_ RowEditing(obje ct sender,
    GridViewEditEve ntArgs e)
    {

    String bedpID = grdBedpDetails. Rows[e.NewEditIndex].Cells[1].Text;
    }

    I have set up the grdBedpDetails. DataKeyNames property
    but this is only useful in the selectedindex event not the RowEditing event

    grdBedpDetails. Rows[e.NewEditIndex].Cells[1].Text return a empty string
    because the column is invisible.

    Thanks




    --
    Life in the sun
  • WayDownUnder

    #2
    RE: C# - ASP - Gridview

    any help Bump...
    --
    Life in the sun


    "WayDownUnd er" wrote:
    [color=blue]
    > Hello,
    > I have a gridview control bound to a objectdatasourc e.
    > The primary key column is on the grid but made visible = false.
    > Because the column is invisible I cant get to the cell value when the user
    > presses
    > the edit button on the grid.
    > Code
    >
    > protected void grdBedpDetails_ RowEditing(obje ct sender,
    > GridViewEditEve ntArgs e)
    > {
    >
    > String bedpID = grdBedpDetails. Rows[e.NewEditIndex].Cells[1].Text;
    > }
    >
    > I have set up the grdBedpDetails. DataKeyNames property
    > but this is only useful in the selectedindex event not the RowEditing event
    >
    > grdBedpDetails. Rows[e.NewEditIndex].Cells[1].Text return a empty string
    > because the column is invisible.
    >
    > Thanks
    >
    >
    >
    >
    > --
    > Life in the sun[/color]

    Comment

    • Walter Wang [MSFT]

      #3
      RE: C# - ASP - Gridview


      Hi,

      Thank you for your post!

      You have two options to get the hidden columns' values depending on whether
      or not the columns belong to the DataKeyNames property of GridView.

      1) If the hidden columns are included in DataKeyNames:

      int id = (int) grdBedpDetails. DataKeys[e.NewEditIndex].Values[0];

      2) If the hidden columns are not included in DataKeyNames:

      Since hidden columns are not rendered to the client, you need to hide the
      columns using CSS rule instead of setting its property Visible="false" .
      Below are the detailed steps:

      a) Create a CSS rule in html header:

      <style type="text/css">
      .hidden { display: none; }
      </style>

      b) Hide a column by applying CSS class to its header and item:

      <asp:BoundFie ld DataField="ID" HeaderStyle-CssClass="hidde n"
      ItemStyle-CssClass="hidde n" />

      c) Then you can get the ID column text by:

      String bedpID = grdBedpDetails. Rows[e.NewEditIndex].Cells[1].Text;


      Hope this helps. If anything is unclear, please feel free to post here.



      Regards,
      Walter Wang
      Microsoft Online Community Support

      =============== =============== =============== =====
      When responding to posts, please "Reply to Group" via your newsreader so
      that others may learn and benefit from your issue.
      =============== =============== =============== =====

      This posting is provided "AS IS" with no warranties, and confers no rights.

      Comment

      • WayDownUnder

        #4
        RE: C# - ASP - Gridview

        Walter,
        Thanks for your reply
        number 1) work great you saved my day again !!

        I have another question releated to the same gridview , I was hoping you
        have a easy answer
        I have column 0 = CommandField ( view)
        I have column 1 = CommandFiled ( edit)

        When the event grdTemp_Selecte dIndexChanged
        occurs on the server I want to know which column was selected
        eg do somthing when view pressed do somthing else when edit pressed.

        Thanks in advance




        --
        Life in the sun


        "Walter Wang [MSFT]" wrote:
        [color=blue]
        >
        > Hi,
        >
        > Thank you for your post!
        >
        > You have two options to get the hidden columns' values depending on whether
        > or not the columns belong to the DataKeyNames property of GridView.
        >
        > 1) If the hidden columns are included in DataKeyNames:
        >
        > int id = (int) grdBedpDetails. DataKeys[e.NewEditIndex].Values[0];
        >
        > 2) If the hidden columns are not included in DataKeyNames:
        >
        > Since hidden columns are not rendered to the client, you need to hide the
        > columns using CSS rule instead of setting its property Visible="false" .
        > Below are the detailed steps:
        >
        > a) Create a CSS rule in html header:
        >
        > <style type="text/css">
        > .hidden { display: none; }
        > </style>
        >
        > b) Hide a column by applying CSS class to its header and item:
        >
        > <asp:BoundFie ld DataField="ID" HeaderStyle-CssClass="hidde n"
        > ItemStyle-CssClass="hidde n" />
        >
        > c) Then you can get the ID column text by:
        >
        > String bedpID = grdBedpDetails. Rows[e.NewEditIndex].Cells[1].Text;
        >
        >
        > Hope this helps. If anything is unclear, please feel free to post here.
        >
        >
        >
        > Regards,
        > Walter Wang
        > Microsoft Online Community Support
        >
        > =============== =============== =============== =====
        > When responding to posts, please "Reply to Group" via your newsreader so
        > that others may learn and benefit from your issue.
        > =============== =============== =============== =====
        >
        > This posting is provided "AS IS" with no warranties, and confers no rights.
        >
        >[/color]

        Comment

        • Mark Rae

          #5
          Re: C# - ASP - Gridview

          "Walter Wang [MSFT]" <wawang@online. microsoft.com> wrote in message
          news:LusFICGhGH A.4688@TK2MSFTN GXA01.phx.gbl.. .
          [color=blue]
          > Since hidden columns are not rendered to the client, you need to hide the
          > columns using CSS rule instead of setting its property Visible="false" .
          > Below are the detailed steps:[/color]

          Much easier simply to unhide the columns temporarily, bind the data and then
          hide the columns again e.g.

          MyGridView.Colu mns[n].Visible = true;
          MyGridView.Data Bind();
          MyGridView.Colu mns[n].Visible = false;

          Saves all that unnecessary messing about with CSS.


          Comment

          • WayDownUnder

            #6
            Re: C# - ASP - Gridview

            Hello Mark,
            Sounds like a good work around.
            But it didnt work for me
            The cell returned empty in the event

            grdBedpDetails_ RowEditin


            grdBedpDetails. Columns[6].Visible = true;
            grdBedpDetails. DataKeyNames = dataKeyNames ;
            grdBedpDetails. DataSourceID = "ODSBedps";
            ODSBedps.Select Parameters.Clea r();
            ODSBedps.Select Method = "GetBedpLis t";
            grdBedpDetails. DataBind();
            grdBedpDetails. Columns[6].Visible = false;

            --
            Life in the sun


            "Mark Rae" wrote:
            [color=blue]
            > "Walter Wang [MSFT]" <wawang@online. microsoft.com> wrote in message
            > news:LusFICGhGH A.4688@TK2MSFTN GXA01.phx.gbl.. .
            >[color=green]
            > > Since hidden columns are not rendered to the client, you need to hide the
            > > columns using CSS rule instead of setting its property Visible="false" .
            > > Below are the detailed steps:[/color]
            >
            > Much easier simply to unhide the columns temporarily, bind the data and then
            > hide the columns again e.g.
            >
            > MyGridView.Colu mns[n].Visible = true;
            > MyGridView.Data Bind();
            > MyGridView.Colu mns[n].Visible = false;
            >
            > Saves all that unnecessary messing about with CSS.
            >
            >
            >[/color]

            Comment

            • Mark Rae

              #7
              Re: C# - ASP - Gridview

              "WayDownUnd er" <waydownunder@n ewsgroups.nospa m> wrote in message
              news:28174C66-2A7B-4DB7-8589-498A7A863BF8@mi crosoft.com...
              [color=blue]
              > Sounds like a good work around.
              > But it didnt work for me
              > The cell returned empty in the event[/color]

              Strange - it has always worked for me. Can you try to set the column's
              visibility to true *immediately* before doing the bind e.g.

              grdBedpDetails. Columns[6].Visible = true;
              grdBedpDetails. DataBind();
              grdBedpDetails. Columns[6].Visible = false;


              Comment

              • Walter Wang [MSFT]

                #8
                RE: C# - ASP - Gridview

                Hi,

                Thank you for your update!

                I've seen you opened a new post regarding this problem in this newsgroup. I
                will reply in that thread. Thank you for your understanding.

                Regards,
                Walter Wang
                Microsoft Online Community Support

                =============== =============== =============== =====
                When responding to posts, please "Reply to Group" via your newsreader so
                that others may learn and benefit from your issue.
                =============== =============== =============== =====

                This posting is provided "AS IS" with no warranties, and confers no rights.

                Comment

                Working...