How to fetch the selected row in a bindingsource - bindingsource

How to fetch the selected row in a bindingsource.
I thought this was the solution:
DataRow row = ((DataRowView)bindingSource1.Current).Row;
But I still get the whole datagridview
If I stand on 'row' open the tree while I'm running my application I can select the Itemarray and see the selected data. but dont know how the change this line
DataRow row = ((DataRowView)bindingSource1.Current).Row;
Could somebody help me solve this problem ?

object row = ((DataRowView)bindingSource1.Current).Row[i];
i is the column index

Never mind, I solved it like this
object[] obj = ((DataRowView)bindingSource1.Current).Row.ItemArray;

Related

Vb.net Move A Datagridview Row to the last row when being edited

I haven't found exactly what i need in the forums. I don't want to move selection or scroll index, all what I need to is to change the entire row with a specific index to be the last line in my datagridview.
I need this to happen automatically when i edit this row.
P.S. Datagrid is Unbound
for example:
dgv.rows(index).index=dgv.row.getlastrow
However, the debugger return an error that index is read only.
Please help guys!
This worked for me:
Dim curr_row As DataGridViewRow = idgv.Rows(Index)
Dim curr_index As Integer=idgv.Rows.GetLastRow(DataGridViewElementStates.Visible)
idgv.Rows.Remove(curr_row)
idgv.Rows.Insert(curr_index, curr_row)

How to Delete DataGridView Rows using RemoveAt?

Im just new to vb.net and I need to delete some specific rows but
DataGridView.Rows(8).Visible = False
does not work I also tried using
DataGridView.Rows.RemoveAt(8)
It deletes the row BUT the problem is when I try to perform this function again there's an erro that says index out of bounds
Please help Im really out of ideas in the moment
Thanks
Try to remove the row at the source (datasource) of your grid not delete the datagridview row
you can try
DataGridView.Rows.RemoveAt(DataGridView.CurrentRow.Index)
this is for Current Row
And you Can Change Index to remove Last Index
DataGridView.Rows.RemoveAt(DataGridView.CurrentRow.Index - 1)
And you Can Change Index to remove Next Index
DataGridView.Rows.RemoveAt(DataGridView.CurrentRow.Index + 1)

Selecting single cell data from selected row - DataGridView

I have a datagridview with data in it, I have set it so you can only select the row and one at a time.
However, I need to get the data from one of the cells in the selected row, I have tried this;
Dim data As String = DataGridView1.SelectedRows.Cells(5).toString
Something like that, but I can't get the value of it, does anyone know the way to do it?
Perhaps the right syntax is:
Dim data As String = DataGridView1.SelectedRows(0).Cells(5).Value.ToString
See MSDN docs
Dim data As String = YourDataGrid.Item("YourColumnHere", YourDataGrid.CurrentRow.Index).Value
hope this help!
SelectedRows requires an index parameter. If you've set it to only select one row, then it will always be 0. Try changing that line to:
Dim data As String = DataGridView1.SelectedRows(0).Cells(5).Value.toString()
'hope this code is also another way............
DataGridView1.SelectedRows(DataGridView1.CurrentRow.Index).Cells(5).Value.ToString()
Rather than use ordinal numbers to identify which row you have selected, if this call is being used inside an event that is related to the datagridview, there should be a value passed to the event called "e" which is a structure of event arguments.
if e.RowIndex is valid for that eventarg structure, you can use it as the value of the row selected:
Dim strCellValue as string = DataGridView1.Rows(e.RowIndex).Cells(6).Value
You can also get e.ColumnIndex from that argument.

Counting total rows on a datagrid

I am trying to count all the rows on a datagrid after it has been populated from my DB. I have searched online on how to do this but nothing seems to work.
here is how I grab my data
test.SelectCommand = Ssql
test.SelectParameters.Clear()
test.DataBind()
where test is my datasource it gets thrown into. Any help is appreciated TY!
My code for this thing is:
TextBox3.Text = DataGridView1.RowCount
it display the number of row populated with some data.
Gets or sets number of rows displayed.
test.RowCount
Can also use.
test.Rows.Count

How do I add records to a DataGridView in VB.Net?

How do I add new record to DataGridView control in VB.Net?
I don't use dataset or database binding. I have a small form with 3 fields and when the user clicks OK they should be added to the DataGridView control as a new row.
If you want to add the row to the end of the grid use the Add() method of the Rows collection...
DataGridView1.Rows.Add(New String(){Value1, Value2, Value3})
If you want to insert the row at a partiular position use the Insert() method of the Rows collection (as GWLlosa also said)...
DataGridView1.Rows.Insert(rowPosition, New String(){value1, value2, value3})
I know you mentioned you weren't doing databinding, but if you defined a strongly-typed dataset with a single datatable in your project, you could use that and get some nice strongly typed methods to do this stuff rather than rely on the grid methods...
DataSet1.DataTable.AddRow(1, "John Doe", true)
I think you should build a dataset/datatable in code and bind the grid to that.
The function you're looking for is 'Insert'. It takes as its parameters the index you want to insert at, and an array of values to use for the new row values. Typical usage might include:
myDataGridView.Rows.Insert(4,new object[]{value1,value2,value3});
or something to that effect.
When I try to cast data source from datagridview that used bindingsource it error accor cannot casting:
----------Solution------------
'I changed casting from bindingsource that bind with datagridview
'Code here
Dim dtdata As New DataTable()
dtdata = CType(bndsData.DataSource, DataTable)
If you want to use something that is more descriptive than a dumb array without resorting to using a DataSet then the following might prove useful. It still isn't strongly-typed, but at least it is checked by the compiler and will handle being refactored quite well.
Dim previousAllowUserToAddRows = dgvHistoricalInfo.AllowUserToAddRows
dgvHistoricalInfo.AllowUserToAddRows = True
Dim newTimeRecord As DataGridViewRow = dgvHistoricalInfo.Rows(dgvHistoricalInfo.NewRowIndex).Clone
With record
newTimeRecord.Cells(dgvcDate.Index).Value = .Date
newTimeRecord.Cells(dgvcHours.Index).Value = .Hours
newTimeRecord.Cells(dgvcRemarks.Index).Value = .Remarks
End With
dgvHistoricalInfo.Rows.Add(newTimeRecord)
dgvHistoricalInfo.AllowUserToAddRows = previousAllowUserToAddRows
It is worth noting that the user must have AllowUserToAddRows permission or this won't work. That is why I store the existing value, set it to true, do my work, and then reset it to how it was.
If your DataGridView is bound to a DataSet, you can not just add a new row in your DataGridView display. It will now work properly.
Instead you should add the new row in the DataSet with this code:
BindingSource[Name].AddNew()
This code will also automatically add a new row in your DataGridView display.