How can get the value of selected row in grid view without events function ?
I using something like that but it doesn't work:
int select= Convert.ToInt32(gvMember.SelectedRows[0].Cells[0].Value.ToString());
Your problem is that you don't actually know how to select a row, you have to select a row by clicking on the row header, or if you want it simpler, just use this code:
gvMember.SelectionMode=DataGridViewSelectionMode.FullRowSelect;
then you can just click on the row itself. That way, it will ensure that there is always at least 1 row selected. If you don't want that full row select mode, you have to check the SelectedRows.Count and notify user to select row by clicking on the row header like this:
if(gvMember.SelectedRows.Count > 0){
int select= Convert.ToInt32(gvMember.SelectedRows[0].Cells[0].Value.ToString());
//... other code
} else {
MessageBox.Show("There is not any row selected, you select row by clicking on the row header!");
}
If you want to get the id corresponding to the current row, you can use the CurrentRow property:
int select= Convert.ToInt32(gvMember.CurrentRow.Cells[0].Value.ToString());
Related
I know how to to do it and put the data on textbox when I select a row from a gridview, What I want to achieve now is when I open a modal pop up form containing a gridview, it will automatically select or extract the date column from the last row of gridview.
This is for the purpose of determining the last date from the record.
Here's what I got so far (under click event for button "ADD")
if (grdSpecificTenantRental.Rows.Count == 0)
{
txtdatefrom.Text = "No record yet";
}
else
{
GridViewRow rowtwo = grdSpecificTenantRental.Rows[grdSpecificTenantRental.Rows.Count - 1];
string index = rowtwo.Cells.ToString();
txtdatefrom.Text = index;
}
Here's the output int he textbox: System.Web.UI.WebControls.TableCellCollection
Obviously this line is incorrect: string index = rowtwo.Cells.ToString();
I want to extract the 4th column in the last row which is the end date
Currently you are getting all the cells that is CellCollection so if you want to get perticular column value then use that array's index to get the required columns value.As you mentioned 4th columns so use the index 3 because index starts from 0.You might want to access it like this,
txtDateFrom.Text = grdSpecificTenantRental.Rows[grdSpecificTenantRental.Rows.Count - 1].Cells[3].Text;
I have a data grid view with a column (combobox column). The following function has been implemented.
Select several rows (click the left most row header and drag).
Programmingly set the value to something (see following). All the selected rows changes.
foreach (DataGridViewRow item in dgv.SelectedRows)
{
item.Cells["cbxxxxx"].Value = p;
}
dgv.EndEdit();
Click the save button to save the changes. However, the last row is excluded in the (dgv.DataSource as DataTable).GetChanges(). The count of the changed data table is always one less than the selected rows. The missing row is the last one (with the black triangle).
How to fix the problem?
The last row presumably is still the active row, so you need to end the edit through the BindingContext:
foreach (DataGridViewRow item in dgv.SelectedRows) {
item.Cells["cbxxxxx"].Value = p;
}
this.BindingContext[dgv.DataSource].EndCurrentEdit();
I have a databound gridview in my winform. I want to know how to get the index of Currently selected rows i.e multiple rows.
I am able to do this with a single row. but is there a way I can have a checkbox or something in which I can index of multiple rows.
The Image below will help u understand better of my requirement.
First set CellContentClick event to your DataGridView.
dataGridView.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.onCellContentClick);
For every cell click it will invoke the following method. Here you can create a list and populate it with clicked row index.
public void onCellContentClick(DataGridViewCellEventArgs cell)
{
// Check whether selected cell is check box column, here 0 indicates the check box column.
if (cell.ColumnIndex == 0)
{
bool isChecked = (Boolean) dataGridView[cell.ColumnIndex, cell.RowIndex].EditedFormattedValue;
if(isChecked)
{
// Below will give you the selected cell row index, for multiple rows you can populate those index in list or whatever you convenient with.
cell.RowIndex;
}
}
}
Use DataGridView.SelectedRows property to get all selected rows.
To select multiple rows:
DataGridView.MultiSelect = true;
i am using master detail grid.when i click on the master grid row with focused row index, it must show the information in detail grid.fr that i have used check box to do that.i have checked checkbox if it is selected or not.but in my context checkbox not used so i have to get through focused row selection.
plz help me i tried for a days on tihis
for (int i = 0; i < ASPxGridView1.VisibleRowCount; i++)
{
object key = ASPxGridView1.GetRowValues(i, "GLOBALID");
if (ASPxGridView1.Selection.IsRowSelected(i))
{
}
}
In your case when you select the row you want to pass the Selected value to your Detailed GridView, you can then get the Selected Row value..
GridView1.SelectedValue // return selected row Data key value
My DataGridView is a single line selection and theres a rowEnter Event where I get the line index every time the selected line changes.
private void rowEnter(object sender, DataGridViewCellEventArgs e)
{
currentRowIndex = e.RowIndex;
}
when I press a delete button I use the same index to delete the row
myDataSet.Avaliado.Rows[currentRowIndex].Delete();
avaliadoTableAdapter.Update(myDataSet.Avaliado);
it works fine if no column in the DataGridView is sorted, otherwise a get an error. What should be the way to know the row index in the dataset that corresponds to the rowindex from the DataGridView?
You don't need to be grabbing the current row index every time a new row is selected. Try something like this instead:
if (_Grid.SelectedRows.Count <= 0) { return; } // nothing selected
DataGridViewRow dgvRow = _Grid.SelectedRows[0];
// assuming you have a DataTable bound to the DataGridView:
DataRowView row = (DataRowView)dgvRow.DataBoundItem;
// now go about deleting the row however you would do that.
If you've got some other sort of data type bound to each row of the grid, simply cast the DataGridViewRow.DataBoundItem to whatever your data type is.
You can find the currently selected row at the time that you are doing the delete:
if(myDataGridView.SelectedRows.Count > 0)
currentRowIndex = myDataGridView.SelectedRows[0].Index;
I usually have the Primary Key for that row as a hidden column (my convention is using the first using column).
I can then ask my persistence Layer to do the rest.