I have a small problem with GridView.
I have a DataTable with at least 5 columns, the size is variant.
I want all columns except one in the DataTable to be visible.
Set the size of the column you don't want to display to 0, then it isn't visable. But why do you want to add a invisable column. Store data somewhere else, it sounds like a bad design.
yeah thought of that but that sounds like a cheap workaround something
that should be easier to do in another way. anyway I had already tried
that but it didn't work... then it just adds a column with 0 length and
doesn't affect the one I want. Maybe I'm not doing it right.
This is a DataTable with values that I put into this GridView with
gridview.datasource = datatable;
gridview.databind();
the gridview is just like this :
<asp:GridView ID="GridView" runat="server" AutoGenerateColumns=true>
<Columns>
<asp:ButtonField CommandName="Enter" Text="Enter" />
</Columns>
</asp:GridView>
when the user is shown the gridview he selects one row and then I need the value in the hidden column.
For those interested in this problem, I have found a solution that at
least I feel is good enough. By this way you can control the gridview
at runtime if you don't know exactly all the columns :D.
just add a onrowcreated to the gridview
protected void GridView_rowCreated(object sender, GridViewRowEventArgs e)
{
// those columns you don't want to display you config here,
// you could use a for statement if you have many :)
e.Row.Cells[1].Visible = false;
// we only need to set the width of the header of the column
if (e.Row.RowType != DataControlRowType.Header)
return;
Good solution. you can further improve it by keeping columns, to be kept invisible in the beginig of the Select Querry for the binding datasource. This way you don't have to reset the width of colums manualy at the end.
it just feels stupid to store values associated with some datagrid in
another place (like session), it should be natural to keep the values
that are connected in the same place.
Is there realy no way of having
autogenereatecolumns=true but still set some column visibility to false
< asp:BoundColumn Visible = False>
< %# DataBinder.Eval(Container.DataItem, "the_columne_name_you_dont_want to_display") % >
< /asp:BoundColumn >
< /COLUMNS >
Note : You should also name these bound columns for accessing them.
Yes this would be the good solution if my column count wouldn't be variant.
There are at least 5 columns and they can be up to 10 and I don't know
until runtime. But the one I don't want to show I know the name of.
I think that you can solve your problem by using Repeater Control.
There are 2 possible way of the problem you have. 1. Set the AutoGenerate property to true and set the datagrid to a data table that only contain the columns you want to display. as concern to you wanna to get the values that are not shown in data grid. There is an other property in DataGrid that is the datamemeber set the primary key column to it and whenever a commond performed on a column get the datamemeber value it will return the key value. and get the value from database you did not showed on the page.
2. Use the Repeater control. If you wanna to know more about Repeater Control please write to me on akbarbuneri@msn.com I will send you an example of the repeater control.
GridView columns
DemonWeb
Cornelius Mostert
Yes, i agree... visible=false in the column you don't want to display and eventually autogeneratedcolumns=false in gridview.
Ema
ShibbyMan666
This is a DataTable with values that I put into this GridView with
gridview.datasource = datatable;
gridview.databind();
the gridview is just like this :
<asp:GridView ID="GridView" runat="server" AutoGenerateColumns=true>
<Columns>
<asp:ButtonField CommandName="Enter" Text="Enter" />
</Columns>
</asp:GridView>
when the user is shown the gridview he selects one row and then I need the value in the hidden column.
Nigel Richardson
just add a onrowcreated to the gridview
protected void GridView_rowCreated(object sender, GridViewRowEventArgs e)
{
// those columns you don't want to display you config here,
// you could use a for statement if you have many :)
e.Row.Cells[1].Visible = false;
// we only need to set the width of the header of the column
if (e.Row.RowType != DataControlRowType.Header)
return;
e.Row.Cells[0].Width = Unit.Pixel(100);
e.Row.Cells[2].Width = Unit.Pixel(200);
}
martin.kolarik
BrianFoote
Hi
So I think this will be easy to you
<columns>
<%#YourMethodInCodeBehind("")%>
</columns>
Code Behind Page
public string YourMethodInCodeBehind(object ob)
{
string str ="";
foreach(DataColumn dcol in _urTable,Columns)
{
str += "<asp:BoundColumn id = "+dcol.ColumnName + " Visible = "+ CheckColumnToBeDisplay(dcol).ToString()+"> < %# DataBinder.Eval(Container.DataItem, "+ dcol.ColumnName +") % > </asp:BoundColumn> "
}
return(str)
}
private bool CheckColumnToBeDisplay(DataColumn dcol)
{
return(true); //your column should be display or not check it here
}
Hope that it will help you
Kyuey
Tony Scarpelli
Is there realy no way of having
autogenereatecolumns=true but still set some column visibility to false
ShahidShah
Hi You may Try < COLUMNS > < asp:BoundColumn > < %# DataBinder.Eval(Container.DataItem, "your_columne_name1") % > < /asp:BoundColumn > < asp:BoundColumn > < %# DataBinder.Eval(Container.DataItem, "your_columne_name2") % > < /asp:BoundColumn > < asp:BoundColumn > < %# DataBinder.Eval(Container.DataItem, "your_columne_name3") % > < /asp:BoundColumn >< asp:BoundColumn Visible = False> < %# DataBinder.Eval(Container.DataItem, "the_columne_name_you_dont_want to_display") % > < /asp:BoundColumn > < /COLUMNS > Note : You should also name these bound columns for accessing them.Millimina
There are at least 5 columns and they can be up to 10 and I don't know until runtime. But the one I don't want to show I know the name of.
ferrethouse
Parser Error Message: Code blocks are not supported in this context.
Hamaze
Hensen
I think that you can solve your problem by using Repeater Control.
There are 2 possible way of the problem you have.
1. Set the AutoGenerate property to true and set the datagrid to a data table that only contain the columns you want to display. as concern to you wanna to get the values that are not shown in data grid. There is an other property in DataGrid that is the datamemeber set the primary key column to it and whenever a commond performed on a column get the datamemeber value it will return the key value. and get the value from database you did not showed on the page.
2. Use the Repeater control. If you wanna to know more about Repeater Control please write to me on akbarbuneri@msn.com I will send you an example of the repeater control.
Thank you.