Datagrid Customization

daniel2milan

Member
Joined
Sep 3, 2005
Messages
7
i have this code in order to customize datagrid but the header text wont appear n only return blank datagrid can some 1 help me?? is there any incorrect syntax on my code??

Code:
    Public Function FillDataGrid(ByVal Sqlstring As String)


        Dim OleDbConn As OleDbConnection = New OleDbConnection(ConnString)
        OleDbConn.Open()

        Dim MyDataSet As DataSet = New DataSet

        Dim MyOleDataAdapter As OleDbDataAdapter = New OleDbDataAdapter
        MyOleDataAdapter.SelectCommand = New OleDbCommand(Sqlstring, OleDbConn)

        MyOleDataAdapter.Fill(MyDataSet)
        Dim tableStyle As New DataGridTableStyle
        DataGrid1.DataSource = MyDataSet

        tableStyle.MappingName = "usher"

        Dim column1 As New DataGridTextBoxColumn
        column1.MappingName = "userID"
        column1.HeaderText = "User Name"
        column1.Width = 30
        tableStyle.GridColumnStyles.Add(column1)

        Me.DataGrid1.DataSource = MyDataSet.Tables(0).DefaultView
        Me.DataGrid1.TableStyles.Add(tableStyle)




        For x As Integer = 0 To MyDataSet.Tables(0).Rows.Count - 1
        ListBox1.Items.Add(MyDataSet.Tables(0).Rows(x).Item("Nama"))
        Next

        StatusBar1.Text = " " & MyDataSet.Tables(0).Rows.Count & " Rows."

        MyOleDataAdapter.Dispose()
        MyDataSet.Dispose()

        OleDbConn.Close()
        OleDbConn.Dispose()

        If Not OleDbConn Is Nothing Then
            OleDbConn.Close()
            OleDbConn.Dispose()
        End If


    End Function
 
Just as the columns have mapping names that need to match, so does the actual datatablestyle!
What I normally do, to make sure they match is, just before adding the table style (but after binding the datagrid) I will do:
tableStyle.MappingName = ((DataTable) dataGrid1.DataSource).TableName

Another method is to create the DataTable with a name:
Dim MyDataTable As DataTable = New DataTable ("usher")

(and then use the datatable as the datasource, rather than one of the datasets tables).

Im not great with vb so ive written the first in C# -> hope it points you in the right direction :)
 
thx for the reply but still wont work.

Public Function FillDataGrid(ByVal Sqlstring As String) is function with sql command input such as "select * from usher"

so the query has been add in

Code:
  Dim MyDataSet As DataSet = New DataSet

        Dim MyOleDataAdapter As OleDbDataAdapter = New OleDbDataAdapter
        MyOleDataAdapter.SelectCommand = New OleDbCommand(Sqlstring, OleDbConn)

        MyOleDataAdapter.Fill(MyDataSet)


The table occur when i add this command

Code:
me.datagrid1.recordsource = mydataset.tables(0)

but when i add some tablestyle on it the changes wont occur and still show the same table not the 1 i expected.

for more information i make the connection manually / programmatically, can tablestyle is implemented on those codings?
 
pheww fortunately ive solved the problem

on the programmaticaly connection you should add

MyOleDataAdapter.Fill(MyDataSet,"usher")

then its work
 
Back
Top