-1

I have a GridView with a ButtonField of type link

 <asp:ButtonField ButtonType="Link" CommandName="more" HeaderText="Name Expands" DataTextField="name" />

When the buttonField is pressed, I want to show one extra cell, in the next column. BUT I don't want to show the whole column, just the cell on that column that belongs to that row.

so i created the following

protected void gv2_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "more")

and e.CommandArgument tells me the number of row that was pressed

if now I say "gv2.Columns[3].Visible = true; the whole extra column goes visible, here i would like to show only one cell from that column.

and if I say:

gv2.Rows[Convert.ToInt32(e.CommandArgument)].Visible = true;   

then the extra column with the field I want stays invisible.

how can I do that?

Many Thanks!

NicoTek
  • 1,127
  • 1
  • 14
  • 34
  • this is the same which i answered here http://stackoverflow.com/questions/7803653/how-to-make-visible-a-column-of-gridview/7804831#7804831 please read AVD and my answer . problem is solved – rahularyansharma Oct 20 '11 at 06:42
  • hey myfriend! that answer would have never showed up because it related to a column and not a cell.... why so angry? – NicoTek Oct 20 '11 at 06:48
  • i am not angry at all, if your column is visible false you can never show of cell . because column is parent .i hope you get and anything else let us know – rahularyansharma Oct 20 '11 at 06:50
  • so how about if I set the column visible = true and then modify each of the cells to false(except the one I want to show).... would that be possible? – NicoTek Oct 20 '11 at 06:57
  • Clearly this is not solved... – NicoTek Oct 21 '11 at 00:19

2 Answers2

1

ts not possible to make cell visible when column's visibility property set to false. so for showing only adjacent cell visible .

you can use the only one column of type template field and then make a table in that template field and set the td of email to visible false and on row command of grid as the previous answers making that lable visible you should make your td visible .

public class Demo
    {
        public string Dept { get; set; }
        public string Email { get; set; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            List<Demo> list = new List<Demo>()
            {
                    new Demo() { Dept="A", Email="a@a.com" },
                    new Demo() { Dept="B", Email="b@b.com" },
            };

            GridView1.DataSource = list;
            GridView1.DataBind();
        }

    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "cmd")
        {
            GridViewRow row = (e.CommandSource as LinkButton).NamingContainer as GridViewRow;
            Label email = row.Cells[1].FindControl("email") as Label;
            email.Visible = true;

        }
    }
rahularyansharma
  • 11,156
  • 18
  • 79
  • 135
  • When trying to use your solution i get this error System.NullReferenceException: Object reference not set to an instance of an object. Pointing to this Line "GridViewRow row = (e.CommandSource as LinkButton).NamingContainer as GridViewRow;" – NicoTek Oct 20 '11 at 06:54
  • because you are using button instead of link button so where i am casting in link button you can cast it as button – rahularyansharma Oct 20 '11 at 07:02
  • Changed that but still same... "System.NullReferenceException: Object reference not set to an instance of an object." pointing to: "GridViewRow row = (e.CommandSource as Button).NamingContainer as GridViewRow;" – NicoTek Oct 20 '11 at 07:05
0

I haven't found a way to do this. Instead I opted for creating another gridview "gv2" that is invisible. When the user clicks one of the buttonfields, In the RowCommand Function I grab the information I need and place it on the invisible grid + the cell I wanted to make visible.

since i'm using AJAX with an "update Panel" it doesn't look too bad.(thats my opinion)

Here you can see how it looks http://www.nicoteksolutions.com/Forms/feed.aspx

Thanks! Nico

NicoTek
  • 1,127
  • 1
  • 14
  • 34