datagrid entry with no db components

M

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.
 
P

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
 
P

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)
 
M

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top