Q1. How do I hide a column in a DataGrid or DataTable?
Ans. DataGrid.Columns(index).Visible = False
See also: Mapping type?
Set DataColumn.MappingType as MappingType.Hidden
ONE MORE VERSION: GRID ONLY:
The grid part of a DataGrid is represented by a table. The TableStyles property provides to this table. TableStyles property returns an object of type DataGridTableStyle, which provides access to all columns in the form of DataGridColumnStyles collection. The GridColumnStyles property of DataGridTableStyle returns it, from where you can get one DataGridColumnStyle. The DataGridColumnStyle represents the style of a column and you need to set Width property of DataGridColumnStyle to 0.
Something like this:
For Each dgt As DataGridTableStyle In myDataGrid.TableStyles
Console.WriteLine(dgt.MappingName)
For Each dgc As DataGridColumnStyle In dgt.GridColumnStyles
dgc.Width = 0
Next dgc
Next dgt
Q2. How do I make DataSet processing faster?
Q3. How do I save only affected rows of a DataSet?
Ans. SELECT * is the worst thing you can do if you want only few columns and your table has many of them. If you want to apply filter later, you can use the Filter property and set a criteria.
The GetChanges method of DataSet returns the changes made to a DataSet since you last saved. This method retruns data in another DataSet. When you call Fill method of DataAdapter, use the DataSet returned by the GetChanges method.
Here is the code:
' Check for changes with the HasChanges method first.
If (Not myDataSet.HasChanges(DataRowState.Modified)) Then
Return
End If
' Create temporary DataSet variable.
Dim xDataSet As DataSet
' GetChanges for modified rows only.
xDataSet = myDataSet.GetChanges(DataRowState.Modified)
' Check the DataSet for errors.
If xDataSet.HasErrors Then
' Insert code to resolve errors.
End If
' After fixing errors, update the DBMS with the DataAdapter
' used to create the DataSet.
myOleDbDataAdapter.Update(xDataSet)
Q3. How do I Get Rid of + Sign in DataGrid?
When I fill my DataSet and display it, all I see is a '+' sign to the left of the grid. When I click on the plus sign I then see my table name. Clicking on the table name finally displays my data. I have played around with all the properties that I thought might be related to this, but with no luck. I want my users to see the data right-away without having to do these extra clicks.
Ans. Try filling data from a DataGrid instead of a DataSet. Means set DataGrid's DataSource property as a DataTable, which you can get from DataSet.Tables property. A DataSet is a collection of DataTables and DataGrid assumes that DataGrid may have more than one tables.
I think this should work:
Private dataGrid1.DataSource = ds.Tables("TableName")
Q4. How do I get a database table's list programmatically?
Ans. Use GetOleDbSchemaTable method of OleDbConnection for OLE DB data sources and Use table name "sysobjects" for SQL Server.
Q5. Is there a way to get the names of all the columns of a table access (or SQL) without using a dataset?
Ans.
Private sc As StringCollection = New StringCollection()
Private r As IDataReader = cmd.ExecuteReader(CommandBehavior.SchemaOnly)
Private drc As DataRowCollection = r.GetSchemaTable().Rows
For Each row As DataRow In drc
sc.Add(row("ColumnName").ToString())
Next row