Hi,
A newbie to vb.net and I am trying to learn this datagrid. Here are my questions:
1. How can I populate a datagrid with selected column of a table without using a query.
2. How can I put a variable column width
3. Can someone show me a code for searching

Thanks in advance.. Pls. it's easier for me to understand if a code is possible.

Newvbguy

Recommended Answers

All 6 Replies

Hi

The way I populate a datagrid:

1- Create a dataset and a dataview.
2 - Get the database.
3 - Fill the dataset with the data from the database table.
4 - Set the dataview table to the dataset.
5 - Set the datasource of the datagrid to the dataview.
6 - Set the table style of the datagrid to show what you want to show.

- The column width you can set it up when setting the table style.
- If you want to filter the data, filter the dataview.

Hope it helps

Oh Thanks. It seems simple but ok, I am interested with the table style. Is this something you can do at design level, in coding/run time. Do you have some codings pls. Thanks again.
newvbguy

Hi

The way I populate a datagrid:

1- Create a dataset and a dataview.
2 - Get the database.
3 - Fill the dataset with the data from the database table.
4 - Set the dataview table to the dataset.
5 - Set the datasource of the datagrid to the dataview.
6 - Set the table style of the datagrid to show what you want to show.

- The column width you can set it up when setting the table style.
- If you want to filter the data, filter the dataview.

Hope it helps

yes sure, the tablestyles can be done with code:

Me.DataGrid1.TableStyles.Clear()

        'Step 1: Create a DataGridTableStyle &  set mappingname to table.
        Dim tableStyle As New DataGridTableStyle
        tableStyle.MappingName = "Table1"

        Dim column As New DataGridTextBoxColumn
        column.ReadOnly = True
        column.MappingName = "ID"
        column.HeaderText = "Event_#"
        column.Width = 50
        column.Alignment = HorizontalAlignment.Center
       tableStyle.GridColumnStyles.Add(column)


        column = New DataGridTextBoxColumn
        column.ReadOnly = True
        column.MappingName = "Date"
        column.HeaderText = "Date_Time"
        column.Width = 110
        column.Alignment = HorizontalAlignment.Center
        tableStyle.GridColumnStyles.Add(column)

        tableStyle.AlternatingBackColor = Color.Lavender

        Me.DataGrid1.TableStyles.Add(tableStyle)

That will add two of the columns ("ID" and "Date") from Table1 to datagrid1. There you can specify the text you want to display, width, etc.

there is more information here if you wnat to go deeper:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwinforms/html/wnf_CustDataGrid.asp

Wow... That's all I need to do. Thanks.... HOW can you this at design time. Is it possible?.
Thanks again.

Newvbguy

yes sure, the tablestyles can be done with code:

Me.DataGrid1.TableStyles.Clear()

        'Step 1: Create a DataGridTableStyle &  set mappingname to table.
        Dim tableStyle As New DataGridTableStyle
        tableStyle.MappingName = "Table1"

        Dim column As New DataGridTextBoxColumn
        column.ReadOnly = True
        column.MappingName = "ID"
        column.HeaderText = "Event_#"
        column.Width = 50
        column.Alignment = HorizontalAlignment.Center
       tableStyle.GridColumnStyles.Add(column)


        column = New DataGridTextBoxColumn
        column.ReadOnly = True
        column.MappingName = "Date"
        column.HeaderText = "Date_Time"
        column.Width = 110
        column.Alignment = HorizontalAlignment.Center
        tableStyle.GridColumnStyles.Add(column)

        tableStyle.AlternatingBackColor = Color.Lavender

        Me.DataGrid1.TableStyles.Add(tableStyle)

That will add two of the columns ("ID" and "Date") from Table1 to datagrid1. There you can specify the text you want to display, width, etc.

there is more information here if you wnat to go deeper:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwinforms/html/wnf_CustDataGrid.asp

That is the way you do it at designt time.
Is that what you mean? is what possible?

oops sorry with my english. What I mean at design time is using the properties of the datagrid. to setup This is the time when you put a datagrid in a form. In other words setting up the table style without using a code.
thnx for the great help. now i am starting to grasp the power of vb.net

newvbguy

That is the way you do it at designt time.
Is that what you mean? is what possible?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.