9 Replies - 21243 Views - Last Post: 25 July 2012 - 06:50 AM Rate Topic: -----

#1 maiOHmai  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 09-July 12

passing data from textbox to datagridview

Posted 24 July 2012 - 07:47 AM

how would i pass textbox value to an empty datagridview?
im using this code:

  
Private Sub btnAddList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddList.Click

        Dim rowNum As Integer = dgvIssuedBooks.Rows.Add()
        dgvIssuedBooks.Rows.Item(rowNum).Cells(0).Value = txtCallNo2.Text
        dgvIssuedBooks.Rows.Item(rowNum).Cells(0).Value = txtTitle2.Text
       
    End Sub




but im getting this error, "No row can be added to a DataGridView control that does not have columns. Columns must be added first."

here is my sample form,

Is This A Good Question/Topic? 0
  • +

Replies To: passing data from textbox to datagridview

#2 maiOHmai  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 09-July 12

Re: passing data from textbox to datagridview

Posted 24 July 2012 - 08:00 AM

here is my sample form,

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#3 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9420
  • View blog
  • Posts: 35,399
  • Joined: 12-June 08

Re: passing data from textbox to datagridview

Posted 24 July 2012 - 08:02 AM

Okay - so create a datatable or dataset with a data table.. add columns to it.. set that as the datasource to the dataview.. add columns to said table/datset with table.. life is good!
Was This Post Helpful? 1
  • +
  • -

#4 maiOHmai  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 09-July 12

Re: passing data from textbox to datagridview

Posted 24 July 2012 - 08:37 AM

i came up with this code:

Private Sub BorrowToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BorrowToolStripMenuItem.Click

        With dgvIssuedBooks
            .Columns.Add("CallNumber", "Call Number")
            .Columns.Add("Title", "Title")
            .Columns.Add("AccessionNumber", "AccessionNumber")
            .Columns.Add("Borrower", "Borrower")
            .Columns.Add("ID", "ID")
        End With

    End Sub


 Private Sub btnAddList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddList.Click
        Dim i As Integer
        With dgvIssuedBooks
            ' Write to cell (0,0)
            .Rows(i).Cells("CallNumber").Value = txtCallNo2.Text
            .Rows(i).Cells("Title").Value = txtTitle2.Text
            .Rows(i).Cells("AccessionNumber").Value = txtAccess2.Text
            .Rows(i).Cells("Borrower").Value = dgvSearch.Rows(i).Cells("FullName").Value
            .Rows(i).Cells("ID").Value = txtShow.Text

        End With
        txtAccess2.Text = ""
        txtCallNo2.Text = ""
        txtTitle2.Text = ""
    End Sub



it works; i can pass textbox value into the datagridview. now, my problem is that, i can only add one row in the dgv. my aim is to, at least insert 3 data into the datagridview. how would i do that?
Was This Post Helpful? 0
  • +
  • -

#5 drec  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 42
  • Joined: 06-July 12

Re: passing data from textbox to datagridview

Posted 24 July 2012 - 08:38 AM

fill in d blanks

Quote

create a datatable or dataset with a data table


Dim table As New ______


Quote

add columns to it


 table.Columns.Add("____", GetType(_____))
 table.Columns.Add("____", GetType(_____))



Quote

set that as the datasource to the dataview


 dgvIssuedBooks.DataSource = ___


add rows / data

 table.Rows.Add(__, __)

Was This Post Helpful? 0
  • +
  • -

#6 drec  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 42
  • Joined: 06-July 12

Re: passing data from textbox to datagridview

Posted 24 July 2012 - 08:46 AM

ok u added data at i = 0
now u click on button to add new data but i is still 0
thats y u r not able to add rows
Was This Post Helpful? 0
  • +
  • -

#7 maiOHmai  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 09-July 12

Re: passing data from textbox to datagridview

Posted 24 July 2012 - 08:56 AM

i've applied your code,

 Private Sub btnAddList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddList.Click
       
        Dim i As Integer
        Dim dt As New DataTable
        dt.Columns.Add("CallNumber", GetType(String))
        dt.Columns.Add("Title", GetType(String))
        dt.Columns.Add("AccessionNumber", GetType(String))
        dt.Columns.Add("Borrower", GetType(String))
        dt.Columns.Add("ID", GetType(String))

        dgvIssuedBooks.DataSource = dt
        dt.Rows.Add(txtCallNo2.Text, txtTitle2.Text, txtAccess2.Text, dgvSearch.Rows(i).Cells("FullName").Value, dgvSearch.Rows(i).Cells("ID").Value)

        txtAccess2.Text = ""
        txtCallNo2.Text = ""
        txtTitle2.Text = ""
    End Sub





and it works great for me, thank you. yet i can only insert one row,. what i want to happened is that, at least 3 records can be added, meaning i must have at least 3 rows in the datagridview. can you help me with that?
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9420
  • View blog
  • Posts: 35,399
  • Joined: 12-June 08

Re: passing data from textbox to datagridview

Posted 24 July 2012 - 09:01 AM

Quote

yet i can only insert one row,. what i want to happened is that, at least 3 records can be added, meaning i must have at least 3 rows in the datagridview.

Well.. you don't want to recreate your table each time, right? so *NOT* having it in the button click event would be a good idea. Perhaps in the load form or form's new? Same with setting the data source.. All you really want in the button click event is that line where you create/add the row.

Now to do it three times - you need to repeat that 'add row' with the different bits of data you wanted to add to the grid.
Was This Post Helpful? 0
  • +
  • -

#9 drec  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 42
  • Joined: 06-July 12

Re: passing data from textbox to datagridview

Posted 24 July 2012 - 11:07 AM

Quote

Perhaps in the load form or form's new? Same with setting the data source.

Public Class Form1

Dim dt As New DataTable

Private Sub Form1_Load .......

dt.Columns.Add("CallNumber", GetType(String))
dt.Columns.Add("Title", GetType(String))
dt.Columns.Add("AccessionNumber", GetType(String))
dt.Columns.Add("Borrower", GetType(String))
dt.Columns.Add("ID", GetType(String))

dgvIssuedBooks.DataSource = dt



Quote

All you really want in the button click event is that line where you create/add the row.

Private Sub btnAddList_Click
dt.Rows.Add(txtCallNo2.Text, txtTitle2.Text, txtAccess2.Text, dgvSearch.Rows(i).Cells("FullName").Value, dgvSearch.Rows(i).Cells("ID").Value)



Note that u r not changing i so

dgvSearch.Rows(i).Cells("FullName").Value and
dgvSearch.Rows(i).Cells("ID").value

will remain same for all rows

also if u want to save d table, lets say to a csv file then ur form1 load event will change

if csvFile exists then
table = read saved csv

dgvIssuedBooks.DataSource = table

and u r setting all columns data type to String
GetType(String)
if d data is int u can use
GetType(Integer)
for datetime
GetType(DateTime)
and so on ...

This post has been edited by drec: 24 July 2012 - 11:14 AM

Was This Post Helpful? 0
  • +
  • -

#10 maiOHmai  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 09-July 12

Re: passing data from textbox to datagridview

Posted 25 July 2012 - 06:50 AM

thank you so much!! it works for me.. :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1