Sultan jahan
Sultan jahan

Reputation: 85

datagridview columns disappears

Guys i have a datagridview and a button on my form. Under form load event following code runs for displaying data into datagridview columns.. I have manually added some columns in datagridview to populate data from dataset.

The button is placed on the form to clear all data except column.. I have tried several times but with data the columns also disappears.. is there any solution for this ? thanks in advance I want to use this datagridview column again for displaying data entered in textbox... without dataset..

con.Open()
cmd.CommandText = "select Code,Name,Operations,Rate,Qty,Tax,Tax_amt,Discount,Total_Amount from products where Id = ('" & CStr(i) & "')"

Dim sdr As SqlDataReader = cmd.ExecuteReader
Dim dt As DataTable = New DataTable()
dt.Load(sdr)
dt.Columns("Code").ColumnName = "ii_code"
dt.Columns("Name").ColumnName = "ii_name"
dt.Columns("Operations").ColumnName = "ii_opera"
dt.Columns("Rate").ColumnName = "ii_rate"
dt.Columns("Qty").ColumnName = "ii_qty"
dt.Columns("Tax").ColumnName = "ii_Tax"
dt.Columns("Tax_Amt").ColumnName = "ii_taxamt"
dt.Columns("Discount").ColumnName = "ii_dis"
dt.Columns("Total_Amount").ColumnName = "ii_total"
DataGridView1.DataSource = dt
con.Close()

Upvotes: 0

Views: 656

Answers (2)

Sultan jahan
Sultan jahan

Reputation: 85

I mean I have one datagridview on my form to view sales order... On form load datagridview is filled with data... But i want to place a button on my form which clears all the data from datagridview to accept new data without deleting data from database.. There are some text box on my form from that new data will be entered temporary on datagridview..

Upvotes: 0

Al-3sli
Al-3sli

Reputation: 2181

If the DataGridView is not bound to any data source, this code will do the trick:

DataGridView.Rows.Clear()

Else you can clear you data source in your example it is data table

 Datatable.rows.clear()

Upvotes: 0

Related Questions