By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,699 Members | 1,372 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,699 IT Pros & Developers. It's quick & easy.

datagrid entry with no db components

P: n/a
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.

Feb 13 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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)

<mh******@gmail.com> schreef in bericht
news:11**********************@g14g2000cwa.googlegr oups.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.

Feb 13 '06 #2

P: n/a
Many thanks.

Feb 14 '06 #3

P: n/a
In addition how can I change the width of this columns I added?

Regards.

Feb 14 '06 #4

P: n/a
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)
Feb 14 '06 #5

P: n/a
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.

Feb 14 '06 #6

P: n/a
Hi have a look at these

http://www.vb-tips.com/default.aspx?...d-faf92f41f563

http://www.vb-tips.com/default.aspx?...9-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)

<mh******@gmail.com> schreef in bericht
news:11**********************@g14g2000cwa.googlegr oups.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.

Feb 14 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.