VB.NET - Datagrid
Asked By Arman
14-Oct-10 02:27 AM
Hi All,
I want to make the datagrid column invisible if that column is empty.
regards,
Arman
Reena Jain replied to Arman
Hi Arman,
try to use this
dgDataGrid.Columns(3).visible = false
here 3 is the index number of your column, column index will always start from 0 (Zero)
Hope this will help you
Anurag replied to Arman
If ur app is windows app. set the column width to zero to hide a column provided you have a non-Nothing tablestyle.
'.... make sure your DataGrid is using a tablestyle
dataGrid1.DataSource = _dataSet.Tables("customers")
Dim dgts As New DataGridTableStyle()
dgts.MappingName = "customers"
dataGrid1.TableStyles.Add(dgts)
'......
'method to set a column with by colnumber
Public Sub SetColWidth(tableStyle As DataGridTableStyle, colNum As
Integer, width As Integer)
Try
tableStyle.GridColumnStyles(colNum).Width = width
tableStyle.DataGrid.Refresh()
Catch 'empty catch .. do nothing
End Try
End Sub 'SetColWidth
'....
' here is how you might call this method
Private Sub button1_Click(sender As Object, e As System.EventArgs)
Dim tableStyle As DataGridTableStyle =
dataGrid1.TableStyles("customers")
SetColWidth(tableStyle, 1, 0) 'set width to zero
End Sub 'button1_Click
See http://bytes.com/topic/visual-basic-net/answers/631535-can-datagrid-column-hidden-vb-net
Rohan Dave replied to Arman
hey Arman -
What you mean by "column invisible if that column is empty " ?
consider the case , you have 100 rows in your gridview. Now out of 100 rows , 10 has some value in the column you want to set invisible. Then in this case you want to show your column or not ?
Lalit M. replied to Arman
When you go to a new row in a DataGrid, the columns display (null). , you can set the
DefaultValue property of the DataColumn to be String.Empty.
Anoop S replied to Arman
Just make the column(s) that is/are empty to zero width.
So if columns 2 and 4 are empty, make them zero width and leave the other columns in the grid at normal width.
You could resize the visible columns to keep the grid the same overall size if you want.
brinda replied to Arman
Hi,
In datasource of datagrid make default data for that column to string.empty.
like dtsoruce is datatable and check is your column name then
dtsoruce .columns("check").DefaultValue = String.Empty
then
dtsoruce .Select("[check] <> ''", String.Empty) if length of result in datarow > 0 then visible true else false.
hope this help.

of the application: Private Sub btnApplyBoundEDB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnApplyBoundEDB.Click 'get connection string Dim connstring As String = MdiMain.FormList.ConnString Dim cnn Dim da As New SqlClient.SqlDataAdapter DSTable.Clear() ' Add table style to grid If (DataGrid1.TableStyles.Count > 0) Then DataGrid1.TableStyles.RemoveAt(0) End If DataGrid1.TableStyles.Add(DataGridTableStyle1) Try cmd.CommandText = "dbo.stp_SelNewRenOverride" cmd.CommandType = CommandType.StoredProcedure cmd.Connection = cnn da ex.Message, MsgBoxStyle.OKOnly) End Try DataGridTableStyle1.MappingName = DSTable.Tables(0).ToString Dim newcol As DataColumn = New DataColumn("Checked", GetType(System.Boolean)) newcol.DefaultValue = True DSTable.Tables(0).Columns.Add(newcol) DataGrid1.DataSource Me.DataGrid1.Size = New System.Drawing.Size(824, 392) Me.DataGrid1.TabIndex = 18 Me.DataGrid1.TableStyles.AddRange(New System.Windows.Forms.DataGridTableStyle() {Me.DataGridTableStyle1}) ' Me.checkc1.MappingName = "Checked" Me.checkc1.HeaderText Clear() I am stuck my friend. Please help me. causing the problem: Dim newcol As DataColumn = New DataColumn("Checked", GetType(System.Boolean)) newcol.DefaultValue = True DSTable.Tables(0).Columns.Add(newcol
the following code: Private Sub btnApplyBoundEDB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnApplyBoundEDB.Click 'get connection string Dim connstring As String = MdiMain.FormList.ConnString Dim cnn Dim cmd As New SqlClient.SqlCommand Dim da As New SqlClient.SqlDataAdapter DSTable.Clear() DataGrid1.TableStyles.Clear() Try cmd.CommandText = "dbo.stp_SelNewRenOverride" cmd.CommandType = CommandType.StoredProcedure cmd.Connection = cnn da.SelectCommand End Try DataGridTableStyle1.MappingName = DSTable.Tables(0).ToString DataGridTableStyle1.AlternatingBackColor = Color.LightBlue Dim newcol As DataColumn = New DataColumn("Checked", GetType(System.Boolean)) newcol.DefaultValue = True DSTable.Tables(0).Columns.Add(newcol) checkc1.MappingName Format = "#, ##0.00;(-#, ##0.00)" DataGridTableStyle1.GridColumnStyles.Add(textboxc9) ' Add table style to grid DataGrid1.TableStyles.Add(DataGridTableStyle1) DataGrid1.DataSource = DSTable.Tables(0) End Sub I get an error: An unhandled dll Additional information: A column named 'Checked' already belongs to this DataTable. Dim newcol As DataColumn = New DataColumn("Checked", GetType(System.Boolean)) newcol.DefaultValue = True = > DSTable.Tables(0).Columns.Add(newcol) Hi, Just table with name then you should not add this column to it. Dim newcol As DataColumn = New DataColumn("Checked", GetType(System.Boolean)) newcol.DefaultValue = True DSTable.Tables(0).Columns.Add(newcol
is my code? Private Sub btnApplyBoundEDB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnApplyBoundEDB.Click 'get connection string Dim connstring As String = MdiMain.FormList.ConnString Dim cnn SqlCommand Dim da As New SqlClient.SqlDataAdapter Dim ds As New DataSet ds.Clear() DataGrid1.TableStyles.Clear() Try cmd.CommandText = "dbo.stp_SelNewRenOverride" cmd.CommandType = CommandType.StoredProcedure cmd.Connection = cnn da.SelectCommand End Try DataGridTableStyle1.MappingName = ds.Tables(0).ToString DataGridTableStyle1.AlternatingBackColor = Color.LightBlue Dim newcol As DataColumn = New DataColumn("Checked", GetType(System.Boolean)) newcol.DefaultValue = True ds.Tables(0).Columns.Add(newcol) checkc1.MappingName Format = "#, ##0.00;(-#, ##0.00)" DataGridTableStyle1.GridColumnStyles.Add(textboxc9) ' Add table style to grid DataGrid1.TableStyles.Add(DataGridTableStyle1) DataGrid1.DataSource = ds.Tables(0) End Sub DataGrid1.Databind() after finishing this sub code. Remove one. Private Sub btnApplyBoundEDB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnApplyBoundEDB.Click 'get connection string Dim connstring As String = MdiMain.FormList.ConnString Dim cnn Dim cmd As New SqlClient.SqlCommand Dim da As New SqlClient.SqlDataAdapter DSTable.Clear() DataGrid1.TableStyles.Clear() Try cmd.CommandText = "dbo.stp_SelNewRenOverride" cmd.CommandType = CommandType.StoredProcedure cmd.Connection = cnn da.SelectCommand End Try DataGridTableStyle1.MappingName = DSTable.Tables(0).ToString DataGridTableStyle1.AlternatingBackColor = Color.LightBlue Dim newcol As DataColumn = New DataColumn("Checked", GetType(System.Boolean)) newcol.DefaultValue = True DSTable.Tables(0).Columns.Add(newcol
have the following: Private Sub btnApplyBoundEDB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnApplyBoundEDB.Click 'get connection string Dim connstring As String = System.Configuration.ConfigurationSettings.AppSettings("FinSolMainDBConn having sbu_id I will have SBU for the first mapping. 'Add tablestyle to grid DataGrid1.TableStyles.Add(ts1) 'Set data source DataGrid1.DataSource = ds.Tables(0).DefaultView Am I correct? I someone help me. Private Sub btnApplyBoundEDB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnApplyBoundEDB.Click 'get connection string Dim connstring As String = System.Configuration.ConfigurationSettings.AppSettings("FinSolMainDBConn End Try 'Dim dt As New System.Data.DataTable("testdatatable") 'Dim dc As System.Data.DataColumn ' Dim dr As System.Data.DataRow Dim textboxc1 As New DataGridTextBoxColumn Dim DataGridTableStyle1 As New this will work also to no avail. C1TrueDBGrid1.DataSource = ds.Tables(0) ' sbu_id 'dc = New DataColumn 'dc.DataType = System.Type.GetType("System.String") 'dc.ColumnName = "SBU" 'dt.Columns.Add(dc) ' profit_id 'dc = New DataColumn 'dc.DataType = System.Type.GetType("System.String") 'dc.ColumnName = "ProfitId" 'dt.Columns.Add(dc) ' sub_id dc = New DataColumn 'dc.DataType = System.Type.GetType("System.String") 'dc.ColumnName = "SUBId" 'dt.Columns.Add(dc) Basically can do with the stock grid is set the DataSource first. This will populate the TableStyles collection of the grid with DataGridColumnStyles. To change the header text you can then enumerate
should it be? Private Sub btnApplyBoundEDB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnApplyBoundEDB.Click 'get connection string Dim connstring As String = MdiMain.FormList.ConnString Dim cnn Dim da As New SqlClient.SqlDataAdapter DSTable.Clear() ' Add table style to grid If (DataGrid1.TableStyles.Count > 0) Then DataGrid1.TableStyles.RemoveAt(0) End If DataGrid1.TableStyles.Add(DataGridTableStyle1) Try cmd.CommandText = "dbo.stp_SelNewRenOverride" cmd.CommandType = CommandType.StoredProcedure cmd.Connection = cnn da Tables(0).ToString If Not DSTable.Tables(0).Columns.Contains("Checked") Then Dim newcol As DataColumn = New DataColumn("Checked", GetType(System.Boolean)) newcol.DefaultValue = True DSTable.Tables(0).Columns.Add(newcol) End If