Itay.B
Itay.B

Reputation: 4119

GridView column width

I'm using VS 2010. I have a GridView with few template columns. I want the 2nd column to not be visible at all, but still to be existed so javascript will be able to see it's value. Does someone knows how to set this width value?

Thanks

Upvotes: 0

Views: 874

Answers (2)

Ken D
Ken D

Reputation: 5968

The Problem:

Your problem originates from the fact that when you hide a data-bound GridView's column, its bounded value is no longer available and if you tried to access it you will get an empty string.

The solution:

Enable 2 events in in your gridview:

RowDataBound: In this event you can access the hidden cell value (before hiding it yet)

protected void MyGridView_RowDataBound(Object sender, GridViewRowEventArgs)
{
   // Here you store the value
   this.sID = e.Row.Cells[1].Text;
}

RowCreated: In this event you hide the cell, write this in the event handler:

protected void MyGridView_RowCreated(Object sender, GridViewRowEventArgs)
{
    // then you hide the cell (Only the cell not the column)
    e.Row.Cells[1].Visible = false;
}

In these codes, after we save the value we need in another variable/array whatever, we can easily hide the cell. You can put that value in a hidden input to enable accessing the value from javascript.

Upvotes: 1

Saw
Saw

Reputation: 6426

Place a HiddenField in the first column and put the value that you need to put it in the second column in it instead of creating the second column.

Upvotes: 2

Related Questions