VB.Net clear datagridview rows without the headers - vb.net

I have a datagridview with columns added from the designer, the data for this grid will be selected from the database and will be directly bound to the grid. For this purpose I have DataPropertyName to the column names of the database table.
I am setting the datasource like this :
dgPayment.DataSource = myDatatable
Now I need to clear the rows of the datagridview without removing the headers of the datagridview. I tried using dgPayment.Rows.Clear(), but this prompted a error because the grid is bound & therefore the rows cannot be manually altered. I also tried setting the datasource to nothing like this :
dgPayment.DataSource = Nothing
But this removes the headers too, which doesn't need to happen because they are the column headers that I added using the designer. How can I clear only the data without clearing the headers.

If you create columns through designer, then in form constructor or in Load event handler put next line of code
dgPayment.AutoGenerateColumns = false
Then freely use dgPayment.DataSource = Nothing for removing all rows

Remove only rows from the datatable.
DataTable.Rows.Clear(). This will keep the columns intact.
Then to refresh the data -
CurrencyManager cm = ((CurrencyManager)) dataGridView1.BindingContext[dataGridView1.DataSource];
cm.Refresh();

Try like this
dataGridView1.DataSource.Rows.Clear()

Related

Datagridview manual column assignments appear in wrong column

I have populated a dataagridview using an SqlDataAdapter dataset and manually assigned fields from the dataset to manually created columns in the datagridview. I'm experiencing something odd, in that for DatagridView.Columns(1), which I assigned as DatagridView.Columns(1).DataPropertyName = "Description", when the grid populates the value from DatagridView.Columns(2).DataPropertyName = "Vers", as so:
Even more perplexing is that datagridview has a SelectionChanged event which populates a textbox with Me.SelectedDescription.Text = DatagridView.SelectedRows(0).Cells(1).Value.ToString, which correctly displays the description:
I'm perplexed as to why the Versvalue is showing in the Description column in the datagridview therefore? This looks like a bug to me or do I need some form of workaround?
Sorry, solved:
I had a hidden column between columns 0 and 1, viewable in the Edit Columns dialog. I needed to move this down to the correct location in the list.

GridView does not update DataTable

I am using a DevExpress XtraGrid/View for windows forms. The datasource of the grid is a binding source, which is connected to a dataset. My problem is, this table adapter does not update the datatable. I can insert new rows without problems, but I can't update. No error message is thrown; when I save changes, the row value just reverts. This happens for every column in the row. Here is my code for saving and then reloading data:
' Class variable
Private _invoiceDetailsAdapter As dsInvoiceDetailsTableAdapters.inv_InvoiceDetailsTableAdapter = New dsInvoiceDetailsTableAdapters.inv_InvoiceDetailsTableAdapter()
'Save Data
InvInvoiceLineBindingSource.EndEdit()
_invoiceDetailsAdapter.Update(DsInvoiceDetails.inv_InvoiceDetails)
'Load
DsInvoiceDetails.inv_InvoiceDetails.Clear()
If Me._invoiceId > 0 Then
_invoiceDetailsAdapter.Fill(DsInvoiceDetails.inv_InvoiceDetails, _invoiceId)
InvInvoiceLineBindingSource.Sort = "LineNum"
End If
I've figured out that it must be the dataset itself, because I've tried using a regular DataGridView with the dataset, to no avail. I generated the dataset through the wizard and had to add ColumnName and SourceColumn properties in the parameters for Insert & Update. The parameters for Insert & Update look identical as far as properties are concerned.
I've also tried creating new datasets, datatables, binding sources and tableadapters. I've even tried a DataAdapter but there was no difference. I have literally spent 2 weeks now looking through the properties and debugging, trying to find a cause.
Can someone please offer some advice?
The problem was DevExpress' XtraGrid functionality.
The XtraGrid does not immediately save an edit value to the linked dataset. The modified row is usually posted to the data object when focus is moved to another grid row. You can programmatically force the update by calling the UpdateCurrentRow method. In your case you should only call the CloseEditor and UpdateCurrentRow methods before updating the dataset via the DB adapter object.
So if a user only updates only one row, those changes go nowhere. Read on for a code snippet...
http://www.devexpress.com/Support/Center/Question/Details/A327

Datagridview should not clear on datasource = nothing

I am loading my datagridview through databinding. After that I would want to allow the user to add more rows to the datagridview.
This is only possible if I make the datasource of the datagridview to nothing.
When I do that , the datagridview clears when I say rows.add command.
How can I add a new row without clearing the data?
If you are using a Data-Bound DataGridView control, you cannot just simply add new row by using the cell property of the control.
A DataGridView that is bound using a DataSet can be access only using the DataSet properties. Forcing it to use the cell property can cause an error "Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound."
Adding new row to a DataGridView control programmatically is useful when you want to pull some data from another table to a Bound DataSet with another table.
These links may help you :
http://social.msdn.microsoft.com/Forums/windows/en-US/c291d580-5a52-422a-b798-fbfb5f799b6a/cannot-add-new-rows-to-a-databound-datagridview-programmatically
http://www.codeproject.com/Questions/411452/Add-Rows-To-Databound-DatagridView

How to keep the data after removing the datasouce from a datagridview in vb.net?

I am populating a DataGridView (grid1) either by DataTable or DataSet.
I can remove a set of selected rows then add them to another unbound DataGridView (grid2).
The problem arises when I take a row from grid2 then add it to grid1.
Rows cannot be programmatically added to the DataGridView's rows
collection when the control is data-bound.
This occurs because
grid1.DataSource = myDataTable
or
grid1.DataSource = myDataSet
but when I do
grid1.DataSource = Nothing
all the rows from grid1 is removed.
Is there anyway to detach a datagridview from its datasource but keep the rows?
I can think of a solution wherein I would add another unbound DataGridView (grid3),
copy the content of the bound datagridview (grid1) to grid3 programmatically
then manipulate it from there.
I haven't tried this, but have a look at the Copy method:
grid1.DataSource = myDataTable.Copy
The MSDN says "Copies both the structure and data for this DataTable."
It may be that copying it also un-binds it.
If you have a databound grid, you should probably not be playing with the rows in the grid at all. Alter the table that you set as the datasource, then the grid will reflect the new rows.
dim row as datarow = myDataTable.newrow()
row.item("Column1") = "new value"
myDataTable.rows.add(row)
now the new row will show up on the databound grid. You can remove rows from the grid by removing it from the datasource datatable as well.

Refreshing a DataGridView after DB has changed?

I need to "refresh" a DataGridView, bound to a database table, on a form within a TabControl
The DataGridView is loaded correctly on startup...
But if the data in the DataBase change, How do I refresh it to reflect new records or updates?
this is what I am doing in code, after looking for some examples on the web:
MyTabBindingSource.EndEdit()
Me.MyTableAdapter.ClearBeforeFill = True
Me.MyTableAdapter.Fill(Me.MyDataSet.MyTable)
MyDataGridView.Update()
MyDataGridView.Refresh()
but nothing changes at all...Do I need to refresh/repaint the TabControl as well as the Form containing it? or what else??
Assuming the datagridview is bound to myTable in the dataset, calling the update() may be the problem. Try getting rid of this.
If that doesn't work try rebinding to mytable again and refresh the DGV.
You shouldn't need to refresh the tab or form.
I'm assuming you have edited the data straight from datagridview and updated it into the server.
You can get/view the updated ones by ticking the "Enable Editing" from datagridview's properties.
Datagridview Tasks
Enable Adding
Enable Editing -- check this one
Enable Deleting
Enable Column Reordering
Once you run your code that clears and fills the datagridview, you will have the new ones.
The way I do this is I clear the DataGridView's DataSource then re-bind it again.
Try:
MyDataGridView.DataSource = Nothing
MyDataGridView.Rows.Clear
MyDataGridView.DataSource = MyTable?