Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataGrid questions 2

Status
Not open for further replies.

shaminda

Programmer
Jun 9, 2000
170
US
I have a data grid on one of my programs. I use the "Data Access Application Block"(DAAB) from Microsoft to talk to the SQL Server database. Here is the code I have so far to Fill the DataGrid:

Dim ds As DataSet = SqlHelper.ExecuteDataset(Connection, CommandType.StoredProcedure, "spLoadUserData")
DataGrid1.DataSource = ds

Here are the questions I have
1) I see the data fine, but I have to press the plus sign and then click the table to see the data. When I load the form I want it to load the data to the form without clicking anything. How do I do this?
2) How do I change the column widths? There are say 5 fields and all 5 fields should be different widths.
3) If the operator selects a row, how do I read it? Also how do I delete the selected row? The form will have a button to delete the selected record. Also the operator should have no access to edit the data. All they can do is select the data and delete it.
 
1. Specify the DataTable contained in ur DataSet.

Code:
DataGrid1.DataSource = ds.Tables(0)

or

DataGrid1.SetDataBinding(ds, "Table")
Note: The ExecuteDataset method does not provide a way to assign custom names to the tables returned. The first table is always numbered 0 and named Table, the second is numbered 1 and named Table1, and so on.

2. In order to change the column width you'll have to create TableStyle for the DataGrid. Visit the link below to get more details. This sample will show you how to auto size the columns.


3. In order to prevent user from editing data in DataGrid, set it ReadOnly property to True.

There are various ways in which u can read the selected row, one of them is
Code:
MessageBox.Show(DataGrid1.Item(DataGrid1.CurrentRowIndex, 0))

MessageBox.Show(DataGrid1.Item(DataGrid1.CurrentRowIndex, 1))

When your DataGrid is bound to DataSet, it shows the data contained within DataTable object embed in DataSet. Removing row from DataTable will remove the row from DataGrid as well.
Code:
'Get the Currency Manager by using the BindingContext of the DataGrid.
Dim myCM As CurrencyManager = CType(Me.BindingContext(DataGrid1.DataSource, DataGrid1.DataMember), CurrencyManager)

myDataSet.Tables(2).Rows(myCM.Position).Delete()


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top