Datagridview should not clear on datasource = nothing - vb.net

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

Related

How do I edit data in a data table that is bound to a combo box without using a datagridview?

I am using a combo box that is bound to a data table by the primary key. By selecting a value from the dropdown I am able to populate several textboxes in a form. I can easily add a new record to the data table. Occasionally I need to edit an existing record but I cannot discover the "index" of the row corresponding to the selection in the combo box even though I can populate the textboxes. Are there any methods to use with "datarow" to determine what record the "Pointer" is pointing to? If so perhaps then I could reference that datarow and write new data to the datatable, then update using a dataadapter... Any help would be greatly appreciated.
You're trying to solve a problem that doesn't exist. You don't need to know the index of the row or the row for that matter. What you should be doing is:
Binding your DataTable to a BindingSource, which you add in the designer.
Binding your BindingSource to your ComboBox AND your TextBoxes.
Select records in the ComboBox and edit in the TextBoxes as you like.
When it's time to save, call EndEdit on the BindingSource to ensure any pending edit is committed to the DataTable.
Use the same data adapter to save the changes from your DataTable as you used to populate it in the first place.
The binding would look something like this:
myDataAdapter.Fill(myDataTable)
myBindingSource.DataSource = myDataTable
With myComboBox
.DisplayMember = "DisplayColumn"
.ValueMember = "PKColumn"
.DataSource = myBindingSource
End With
myTextBox.DataBindings.Add("Text", myBindingSource, "EditColumn")
The saving would look something like this:
myBindingSource.EndEdit()
myDataAdapter.Update(myDataTable)
At no point do you have to care what rows were edited and where they are in the DataTable because Update will just save all changes.

VB.Net clear datagridview rows without the headers

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()

Turning datagrid values that dont match datasource to datatable

I have a datagrid that gets an extra column with checkboxes added that is not in its source. I am trying to convert the grid that the user sees to a datatable when a save button is clicked. What I am trying to do is run it through a for loop and check if the rows checkbox is checked and the best solution seems to be converting it to a datatable and checking from there. Most of what I have seen to do the conversion is Dim dt As DataTable = DirectCast(DirectCast(dtgrd, DataGrid).DataSource, DataTable) which wont work for me since the checkbox values that I am checking for are not in the datasource. I am trying to avoid a million postbacks so I am not updating anything when the checkbox changes.

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.

Duplicate a DataGridView on a second windows form

I have a bound DGV that took a bit of work to get its columns set up. I'd like to show a 1-row version of this identical DGV on a second windows form. Is there a way to programatically place a copy on the second form. I would adjust the height and position of the 1-row version, and create a new binding source on the second form so that I could filter the data.
MyForm.Controls.Add(myDataGridView)
So further explanation:
In your first for you will need to make a variable or property that contains a reference to the DataGridView that you want to access.
I'd suggest doing something like this.
Public Shared Property myDataGridView As DataGridView
then after you get it set up in the form the way you want it set up
myDataGridView = originalDataGridView
Then in the second form
SecondForm.Controls.Add(FirstForm.myDataGridView)
Will add the DataGridView exactly as it is on the first form.
Edit
If you are creating it in a designer, you can just either copy and past it from the original for to the second form.
Or just on the Form.Shown or in the New() of the first form set the myDataGridView to the DataGridView that you created.