8 Replies - 4077 Views - Last Post: 05 April 2013 - 08:38 AM Rate Topic: -----

#1 vks.gautam1  Icon User is offline

  • D.I.C Regular

Reputation: 17
  • View blog
  • Posts: 317
  • Joined: 21-March 08

Data Table with DataGridview

Posted 31 March 2013 - 12:53 AM

 Dim dt As New DataTable



Private Sub frmMedicineWholesale_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dt.Columns.Add("Item_Id", GetType(Integer))
        dt.Columns.Add("item_name", GetType(String))


end sub

 Private Sub BTNOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNOK.Click

        
        Dim r As DataRow = dt.NewRow
        r("Item_Id") = TxtItemId.Text
        r("Item_name") = txtitem_name.Text
   dt.Rows.Add(r)
        
        frmwholesalebilling.DataGridView1.DataSource = dt
 
    End Sub





i want to add data to datagridview..dont want to overwrite existing data of datagridview or datatable

Is This A Good Question/Topic? 0
  • +

Replies To: Data Table with DataGridview

#2 vks.gautam1  Icon User is offline

  • D.I.C Regular

Reputation: 17
  • View blog
  • Posts: 317
  • Joined: 21-March 08

Re: Data Table with DataGridview

Posted 31 March 2013 - 02:41 AM

data is lost due to im collecting information from many forms to medicine whole sale form. after medicine wholesale form .im adding data to datagridview to final form and if i want more data to add..im have to go again to medicinewholesale form.. any idea how to use funtion in module for all this
Was This Post Helpful? 0
  • +
  • -

#3 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Data Table with DataGridview

Posted 31 March 2013 - 09:08 AM

So is it that you want to also be able to add to the datatable from the final form but you have lost dt due to switching forms?

I'm confused with your situation.

You can always attempt to cast the datasource of the datagridview back to a datatable where you can perform additional newrow(s)

Please describe your problem more clearly so that we can help you further.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3253
  • View blog
  • Posts: 10,908
  • Joined: 12-December 12

Re: Data Table with DataGridview

Posted 31 March 2013 - 09:56 AM

I suspect the immediate problem is caused by changing the DataSource of the DGV, which wipes-out all previous data. As CharlieMay suggests you might cast the DataSource to a DataTable in order to add a new row. This will also, I believe, avoid the need to create a (very) temporary DataTable.

However, a more substantial issue is, I suspect, that there are (inter-)dependencies between all these forms. Essentially, there should be one main form that opens (spawns) other forms (some of which might be modal, or dialogs), each time subscribing to an event of the new form(s), so that information can be passed between them. There are a number of ways to pass this information in a robust, correct, manner: you might search "passing data between forms".
Was This Post Helpful? 1
  • +
  • -

#5 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3253
  • View blog
  • Posts: 10,908
  • Joined: 12-December 12

Re: Data Table with DataGridview

Posted 31 March 2013 - 10:12 AM

..but is it not possible to add a row to a DataGridView directly?

Me.DataGridView1.Rows.Add("Blah1", "Blah2")

Was This Post Helpful? 1
  • +
  • -

#6 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Data Table with DataGridview

Posted 31 March 2013 - 10:44 AM

This was actually going to be my first response but I wasn't quite clear on the problem. ;)
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3253
  • View blog
  • Posts: 10,908
  • Joined: 12-December 12

Re: Data Table with DataGridview

Posted 31 March 2013 - 10:51 AM

View PostCharlieMay, on 31 March 2013 - 05:44 PM, said:

This was actually going to be my first response but I wasn't quite clear on the problem. ;)

I quite often (as, I am sure, do others) investigate a detailed solution, only to discover a more direct route :). [Strangely, I don't mind this so much, as I learn a lot in the process.]

However, as mentioned, I suspect the OP has more issues than this simple addition of a row.
Was This Post Helpful? 0
  • +
  • -

#8 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Data Table with DataGridview

Posted 31 March 2013 - 11:00 AM

I agree. As you can see, the op is using the default instance of the form that contains the datagridview to set the datasource. Therefore, why even create a datatable at all and just use the method you displayed in post 5 to populate the dgv? That's why I think you nailed the root of the problem in post 4.
Was This Post Helpful? 0
  • +
  • -

#9 vks.gautam1  Icon User is offline

  • D.I.C Regular

Reputation: 17
  • View blog
  • Posts: 317
  • Joined: 21-March 08

Re: Data Table with DataGridview

Posted 05 April 2013 - 08:38 AM

I got solved this..i was declaring data table again and again on button click so i was losing data.after reading your responses i have got more few ways to tackle my problem..i will work on your given ideas..
Thnx guys and sorry for responding so late

This post has been edited by vks.gautam1: 05 April 2013 - 08:39 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1