VB.NET - datagrid column invisble with autogenerate column set to false

Asked By sarah lee
08-Dec-06 11:29 AM

hi all

i want to make the first column in datagrid to be set to say ProductID.

but i dont want it to be visible.

i have generated all other columns with code(autogenerate column is set to false)

how can i do this?

also i want the value of that invisible first column to be used as parameter to a function

can anyone please help me

thanks

DataGridColumn.Visible  DataGridColumn.Visible

08-Dec-06 11:34 AM

>> but i dont want it to be visible. <<

To make the first column invisible, simply set the Visible property of the column to false:

DataGrid1.Columns[0].Visible = false;

how to  how to

08-Dec-06 12:49 PM
You can set the visibility attribute to false.
example: <asp:BoundColumn Visible="false"...../>

Also another way how to remember the key id is to store it as parameter of your "button" or "linkbutton" and bind it inside ItemDataBound event. Here is an example:

void dgTRFEntries_ItemDataBound( object sender , DataGridItemEventArgs e )
    {
        if( e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem )
        {
          LinkButton View = (LinkButton) e.Item.FindControl( "View" );
         
           View .CommandArgument = DataBinder.Eval( e.Item.DataItem , "keyID " ).ToString();
        }
    }


LinkButton part of datagrid:
<asp:TemplateColumn ItemStyle-Wrap="false" HeaderText="Action">
                                    <ItemTemplate>
                                        <asp:LinkButton id="Edit" runat="server" />

datagrid column invisble with autogenerate column set to false  datagrid column invisble with autogenerate column set to false

08-Dec-06 01:21 PM
To hide few columns in the datagrid we can make use of the tag <asp:boundcolumn> assign the relevent values to the attributes DataField,HeaderText and set the visible property to true/false

<asp:datagrid id="DataGrid1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="ProductName" HeaderText="
ProductName"></asp:BoundColumn>
<asp:BoundColumn DataField="
ProductID" HeaderText="ProductID" visible="false"></asp:BoundColumn>
</Columns>
</asp:datagrid>

datagrid column visible false true  datagrid column visible false true
09-Dec-06 02:14 AM

Hi,
The bellow examples i gave for ur reference like that also you can do....i hope you know the way of database connection,if yes this code will help you.


 <asp:GridView ID="showImage_grid" runat="server" AutoGenerateColumns="False" BorderStyle="None" GridLines="None">
                                                <Columns>
                                                    
                                                    <asp:TemplateField>
                                                        <ItemTemplate>
                                                            <asp:Label ID="id" Text='<%#Eval("userid") %>' runat="server" Visible="false"></asp:Label>
                                                        </ItemTemplate>
                                                       
                                                    </asp:TemplateField>

                                                        <asp:TemplateField>
                                                        <ItemTemplate>
                                                            <asp:Label ID="id" Text='<%#Eval("username") %>' runat="server" ></asp:Label>
                                                        </ItemTemplate>
                                                        <ItemStyle VerticalAlign="Bottom" />
                                                    </asp:TemplateField>
                                                </Columns>
                                                <PagerSettings Mode="NumericFirstLast" />
                                            </asp:GridView>

Hiding auto geerated Columns In A DataGrid  Hiding auto geerated Columns In A DataGrid
09-Dec-06 05:49 AM

this will not work :
 datagrid1.columns(columnnumber).visible=false

instead you have to bind the ItemDataBound event and using the DataGridItemEventArgs item to hide the dynamic column. for eg :

private void dg_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
    e.Item.Cells[3].Visible = false;
}

Beginner needs more assistance  Beginner needs more assistance
13-Dec-06 10:12 PM

Kevin k,

I am very new to vb.net and need some help knowing just where to place the code you provided for hiding the column in a datagrid control.

I want to hid the ID field that I have in my datagrid, but I want it to be bound to the control so that when the user selects a row, I can take the appropriate action.

Your assistance is appreciated.

Hind Columns in Datagrid  Hind Columns in Datagrid
14-Dec-06 04:23 AM

goto Datagridproperties , from events select ItemDataBound Event

by doule clicking on that event ItemDataBound event added to code behind page

here dg is datagridname

 

   e.Item.Cells[3].Visible = false;

here index 3 is column index, hiding Column 4 in datagrid


private void dg_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
    e.Item.Cells[3].Visible = false;
}

hope now u got how to do it

praveen

Event not available  Event not available
14-Dec-06 07:29 AM

I do appreciate the response. However, I am not able to implement your description of what to do.  When I go to the code behind the form and select the datagrid, I do not have an "ItemDataBound" event available from the list of datagrid events. 

If it makes any difference, I am using VB.net 2003 and developing a Windows Form.  Would it be advantageous to upgrade to VB.net 2005?  The code almost looks as if it is something that would be used in a web form.

windows application or webapplication  windows application or webapplication
14-Dec-06 08:35 AM
end of post
Windows application  Windows application
14-Dec-06 09:47 AM
I am developing a Windows Application and using VB.net 2003.
Hide a Column in DataGrid in two ways  Hide a Column in DataGrid in two ways
14-Dec-06 01:28 PM

1) You can use your DataSet's ColumnMapping property to hide a column.      // Creating connection and command sting
 
     string conStr = @"Provider=Microsoft.JET.OLEDB.4.0;data source=C:\northwind.mdb";
 
     string sqlStr = "SELECT * FROM Employees";
 
     // Create connection object
 
     OleDbConnection conn = new OleDbConnection(conStr);
 
     // Create data adapter object
 
     OleDbDataAdapter da = new OleDbDataAdapter(sqlStr,conn);
 

 
     // Create a dataset object and fill with data using data adapter's Fill method
 
     DataSet ds = new DataSet();
 
     da.Fill(ds, "Employees");
 

 
     // Hide the column and attach dataset's DefaultView to the datagrid control
 
     ds.Tables["Employees"].Columns["LastName"].ColumnMapping = MappingType.Hidden;
 
     dataGrid1.DataSource = ds.Tables["Employees"];

2)
You must first declare an new DataGridTableStyle object.
Then set the .Mappingname = your Datasets tablename.
Add the datagridtablestyle object to the grid by using

Datagrid.TableStyle.Add
Declare a GridColumnStyleCollection
Set GridCSCObject = DataGrid.TableStyles(index of your created
tablestyleobject).GridColumnStyle
Then you can add and remove columns by using
MyGridColumnStyleCollectionObject.
Ex: MyGridColumnStyleCollectionObject.RemoveAt(ColumnIndex) hides the column
at position ColumnIndex.

Praveen

 

Your help appreciated  Your help appreciated
14-Dec-06 02:55 PM

Thanks for being patient with a beginner.

Also, thanks for being so thorough.

I'll be giving this a try later this evening.

Mr. B

Create New Account
help
VB net, datagrid button I want to add a button to a datagrid. I know that it can be done in ASP.net, but I want to add a button in VB.net (windows app.) with Oracle as my backend. I create a dataset and populate the dategrid with it, then I format the datagrid to limit the amount of data shown. But I want to put a button on every row in the last column of my datagrid that will load another form which will be based on a field
VB NET datagrid the right tool? How to put it to Hi all, I've got some specific questions about the datagrid. I'm using Microsoft Visual Studio .NET 2003 to develop an application. I'm new to VB.Net and I've got a hard time with the differences between developing with VB.Net and developing with MS Access VBA (the latter I've been using till now on
vb.net datagrid view hi i'm beginner in vb.net 2005 in visual studio 2005 and i have datagrid view that is connected to ms access database and i also have textbox and buttom write a name in the textbox and and on button press the name in the datagrid view is to be highlighted and this is my code: Public Class Form1 Dim da row using a nice little extended class and then call the select method of the datagrid. . In a project with a form add 1 datagrid called datagrid1 1 textbox called textbox1
deleting a row in datagrid vb.net Hi, Am a newbie to vb.net. . In my application am using a datagrid.I have retrieved the values from database on formload.Now i want to know how to select a particular row in datagrid, and after selecting when i click the delete button the row should get selected. . . . Database aspx http: / / www.syncfusion.com / faq / windowsforms / Default.aspx#44 In case of ASP.NET datagrid, if you want the DataGrid's DeleteCommand event to fire whenever a row is clicked
Datagrid VB NET How can i focus / posistion my cursor to particular (row, column) in a datagrid you need to get the DataGridCell corresponding to the row and column and make the CurrentCell property of the DataGrid to be this cell. / / C# Untested Code DataGridCell dc = new DataGridCell(row column); DataGrid1.CurrentCell = dc; to the current cell as Dim myPoint As String = myDataGrid.CurrentCell.ColumnNumber