Home All Groups Group Topic Archive Search About

datagrid entry with no db components

Author
13 Feb 2006 8:16 AM
mhmtzdmr
Hi,

How can I insert an entry to datagrid without using db components? For
example, I have a textbox and I would like to press OK button and
insert the text in textbox to datagrid.

Regards.

Author
13 Feb 2006 9:29 AM
Peter Proost
Hi you need a datatable for that,
for example:

Dim dt As New DataTable("MyTable")
dt.Columns.Add("Name")
dt.Columns.Add("Country")
Datagrid1.DataSource = dt.DefaultView

you then can add rows with a datarow object or with

dt.LoadDataRow(New Object() _
            {"Peter", "Belgium"}, True)
dt.LoadDataRow(New Object() _
            {"Cor", "The Netherlands"}, True)
dt.LoadDataRow(New Object() _
            {"Ken", "USA"}, True)
dt.LoadDataRow(New Object() _
            {"You", Textbox1.Text}, True)

Hope this helps

Greetz Peter
--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning. (Rich Cook)

<mhmtz***@gmail.com> schreef in bericht
Show quoteHide quote
news:1139818599.729279.265010@g14g2000cwa.googlegroups.com...
> Hi,
>
> How can I insert an entry to datagrid without using db components? For
> example, I have a textbox and I would like to press OK button and
> insert the text in textbox to datagrid.
>
> Regards.
>
Author
14 Feb 2006 7:10 AM
mhmtzdmr
Many thanks.
Author
14 Feb 2006 7:20 AM
mhmtzdmr
In addition how can I change the width of this columns I added?

Regards.
Author
14 Feb 2006 7:40 AM
Peter Proost
Hi, you can use a DataGridTableStyle and DataGridTextBoxColumn for that (see
code below)

Hope this helps

Greetz Peter

Dim dt As New DataTable("MyTable")
        dt.Columns.Add("Name")
        dt.Columns.Add("Country")


        Dim ts1 As New DataGridTableStyle
        ts1.MappingName = "MyTable" 'Has to be the same as the table name
              Dim TextCol As DataGridTextBoxColumn
            For i As Integer = 0 To dt.Columns.Count - 1
            TextCol = New DataGridTextBoxColumn
            Select Case i
                Case 0
                    TextCol.Width = 50
                    TextCol.Alignment = HorizontalAlignment.Center
                    TextCol.HeaderText = dt.Columns(i).ColumnName
                Case 1
                    TextCol.Width = 100
                    TextCol.Alignment = HorizontalAlignment.Center
                    TextCol.HeaderText = dt.Columns(i).ColumnName
                    TextCol.NullText = ""
            End Select

            TextCol.NullText = ""
            TextCol.MappingName = dt.Columns(i).ColumnName
            ts1.AllowSorting = False
            ts1.GridColumnStyles.Add(TextCol)
        Next

        DataGrid1.TableStyles.Clear()
        DataGrid1.TableStyles.Add(ts1)

        DataGrid1.DataSource = dt.DefaultView

        dt.LoadDataRow(New Object() _
                    {"Peter", "Belgium"}, True)
        dt.LoadDataRow(New Object() _
                    {"Cor", "The Netherlands"}, True)
        dt.LoadDataRow(New Object() _
                    {"Ken", "USA"}, True)
        dt.LoadDataRow(New Object() _
                    {"You", "Your Country"}, True)
Author
14 Feb 2006 8:49 AM
mhmtzdmr
Hi Peter,

Thanks for your time and helps.

Last question: Is it possible to  use combobox on datagrid? For example
I want to add the values to be selected from the combobox manually.

Best Regards.
Author
14 Feb 2006 12:11 PM
Peter Proost
Hi have a look at these

http://www.vb-tips.com/default.aspx?ID=1eb83247-7464-467e-a9dd-faf92f41f563

http://www.vb-tips.com/default.aspx?ID=af5ec1a9-bd98-4556-adf9-3866d4936de0

Greetz Peter

--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning. (Rich Cook)

<mhmtz***@gmail.com> schreef in bericht
Show quoteHide quote
news:1139906993.777149.157140@g14g2000cwa.googlegroups.com...
> Hi Peter,
>
> Thanks for your time and helps.
>
> Last question: Is it possible to  use combobox on datagrid? For example
> I want to add the values to be selected from the combobox manually.
>
> Best Regards.
>