Set Focus on row by cell value - c#

I have two grid views namely PositionsReadyListGridView and PositionsNotReadyListGridView.
Now the functionality requirement is on click of Button Set Not Ready the selected item from PositionsReadyListGridView is removed from this list and added to PositionsNotReadyListGridView.
Similarly on click of Button Set Ready the selected item from PositionsNotReadyListGridView is removed from this list and added to PositionsReadyListGridView.
I have implemented this functionality but I am unable to set Focus on the latest row which is added to the either of the GridView.
Is there a way that I can set Focus to the row according to cell
values?
For example in both of the Grids I have a column colID which is unique to a row.
Can I somehow use this ID to set Focus to the row added to either PositionsReadyListGridView (on Set Ready click) or PositionsNotReadyListGridView (on Set Not Ready Click)?
Thanks

You can use LocateByValue method, which returns RowHandle of located row and set this value to FocusedRowHandle property:
int rowHandle = PositionsReadyListGridView.LocateByValue("colID", ID);
if (rowHandle != GridControl.InvalidRowHandle)
PositionsReadyListGridView.FocusedRowHandle = rowHandle

to get the lately added row get it by
PositionsReadyListGridView.Rows.Count - 1
and for setting the focus
PositionsReadyListGridView.Rows[PositionsReadyListGridView.Rows.Count - 1].Cells[colID].Selected = true;

private void PositionsNotReadyListGridView_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
this.PositionsNotReadyListGridView.Rows[e.RowIndex].Selected = true;
}

for devExpress use this code :
gridView1.FocusedRowHandle = gridView1.LocateByValue("columnName",value of columnName, null);

Related

WinForm - DataGridView when row change event

I currently have an event set for RowHeaderMouseClick when I click on the header, my textbox is populated with data from the DataGrid. I want to make my textbox populate when I select the row instead of the header. Ideally, I want to hide the header. What is the correct event/property that I need to set to achieve this?
Edit:
Attaching screenshot
You can make like this for any event
//Handle RowChanged.
table.RowChanged += table_RowChanged;
//RowChanged Event.
static void table_RowChanged(object sender, DataRowChangeEventArgs e)
{
Console.WriteLine("... Changed: " + (int)e.Row["Dosage"]);
}
You don't need to do it with events, as you seem to be saying that the textbox gets populated with an item from the row. In such a case you would have:
your datagridview bound to a bindingsource
your bindingsource bound to a datatable or other list that supports binding
your textbox's text property bound to the same bindingsource
Every time the user clicks a row in the grid (or uses the keyboard to move to another row) they are causing the Current property of the bindingsource to update. This in turn changes any of the textboxes that are bound to true same binding source (a textbox only shows the current row item to which it is bound)
For a quick demo of how this works, do these steps (apologies I can't make any screenshots - I'm on a cellphone) - skip any steps you've already done
add a DataSet type file to your project
open it and right click the surface, add a datatable and name it eg Person
right click it and add a couple of columns eg FirstName and LastName
save
switch to the form
open the Dat Sources window (view menu, other windows)
drag the Person node to the form, a datagridview appears as well as some other stuff (bindingsource) - look at the DataSource property of the grid
in the Data sources window again click the drop down button next to Person, change it to details
drag the person node to the form again, this time textboxes appear; take a look at their Text bindings in the (data bindings) section of their properties - they're bound to the same bindingsource as the grid is
run the project, type 4 names into the grid and then select different rows at random using the mouse; the textboxes update to stay current with the grid selection
If this isn't the way you've done things up to now you should consider making it the way; using the DataSet designer to create strongly typed datatables is an easy way to model the data aspects of your program and there is a lot of tooling set up to make life easier when you use them to make a data driven app. If you've been putting data directly into a datagridview it's something you should avoid going forward, and instead separate your concerns in a more mvc style pattern
If you want to remove or make the rowHeader invisible
You can SET an Event DataGridView CellClick and below is the CODE
//Im Assuming that we are getting the row VALUES
try{
int rowIndex = e.RowIndex; //getting the position of ROW in DGV when CLick
int columns = Columns.Count//numebr of Columns
for(int i=0; i< columns;i++)
{
//Here we can populate textBoxes and using FlowLayoutPanel
string values = dataGridView1.Rows[rowIndex].Columns[i].Value.toString();
//Creating new TextBox
TextBox txtBox = new TextBox();
txtBox.Text = values;
txtBox.Size = new Size(100,200);
flowLayoutPanel1.Controls.Add(txtBox);
}
}catch(Exception ee){}
OR If you want to pass the VALUES of the selected ROW you can do like this
textBox.Text = dataGridView1.Rows[rowIndex].Columns[0].Value.toString();//1st col
textBox1.Text = dataGridView1.Rows[rowIndex].Columns[1].Value.toString();//2nd col
First, right-click your dataGrid and select properties
Then change the SelectionMode to FullRowSelect if you like as shown below:
Next, In the datagrid event section double-click on SelectionChanged
and write code like this, you can use other events, although
// Just for example
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
try
{
if (dataGridView1.CurrentRow != null && dataGridView1.CurrentRow.Index >= 0)
{
var row = dataGridView1.CurrentRow;
txtCode.Text = (row.Cells["code"].Value != null) ? row.Cells["code"].Value.ToString() : string.Empty;
txtFirstName.Text = (row.Cells["firstName"].Value != null) ? row.Cells["firstName"].Value.ToString() : string.Empty;
txtLastName.Text = (row.Cells["lastName"].Value != null) ? row.Cells["lastName"].Value.ToString() : string.Empty;
}
}
catch { }
}

WPF DataGrid Move to last selected row without highlight recent clicked row

I have DataGrid and added event as below, performing some calculation for validation, if validation is success then nothing to do. But if validation get failed then highlighted row should be still last selected row, not newly clicked row:-
public void dataGridTable_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (dataGrid.SelectedIndex == lastSelectedIndex)
{
return;
}
/*
Validate method will return true if row validation is fine else false.
*/
if(!validate())
{
// Revert to last selected index.
dataGrid.SelectedIndex = lastSelectedIndex;
}
}
dataGrid.SelectedIndex = lastSelectedIndex; this code will make selected index as a last selected index (at c# code i check it in debug mode) but in WPF DataGrid still highlight newly clicked row.
Please let me know if i am not clear enough.
Thanks
You can clear the selected cells using the DataGrid.SelectedCells property.
Just do
this.SelectedCells.Clear();
to clear the current selection.
Or if you want to select cells in the current row of the SelectedIndex you can do
this.SelectAllCells();
DataGridCellInfo[] cells = this.SelectedCells.Where(x => this.Items.IndexOf(x.Item) == this.SelectedIndex).ToArray();
this.SelectedCells.Clear();
foreach (DataGridCellInfo cell in cells)
{
this.SelectedCells.Add(cell);
}
Where this is the instance of the DataGrid.

Update Devexpress ComboBoxItemCollection items in the gridview mouse down event

I am using Devexpress Xtragrid Gridcontrol to show stuff in the grid. Each row shows the values of one object, which is represented as text cell besides two comboboxes. To represent the comboboxes I am using repositoryItemComboBox and ComboBoxItemCollection. I have also defined this event for the gridview
prjGridView_MouseDown(object sender, MouseEventArgs e)
{
var hitInfo = prjGridView.CalcHitInfo(e.Location);
if (hitInfo.InRowCell)
{
int rowHandle = hitInfo.RowHandle;
GridColumn column = hitInfo.Column;
if (hitInfo.Column.Name.Equals("UsersItems"))
{
//Update the cell combobox data
}
How can I get the control in the cell shown in the hitInfo. I need this to update the values of the combobox in that cell, each "UserItems" combobox can have different items.
thanks,
ES
You can use ActiveEditor property of gridview.
var activeEditor = prjGridView.ActiveEditor;
Found a very simple method.
I defined this in the class
private ComboBoxItemCollection phaseColl
within the molusedown event I simply use the RowHandle to extract the correct object that is being represented in the row and update the combobox. I think this is a very simple implementation since one cannot look at multiply controls at the same time this will work.
-es

DataGridView selecting a specific row and retrieving all column values to an object array

I want to perform operation that On a button click event , Grid Currentrow entire data is passed to an object array
I have tried to search through following links :
DataGridView selecting a specific row and retrieving its values
Getting data from selected datagridview row and which event?
But they are talking about particular cell value
i tried to perform with code
DataRowView currentDataRowView = (DataRowView)grdGLSearch.CurrentRow.DataBoundItem;
DataRow row1 = currentDataRowView.Row;
But currentDataRowView is retrieving null
one of My Senior succesfully created a generic property GetSelectedRow()
it works like this :
var object =grdGLSearch.GetSelectedRow<T>();
and it has definition
public T GetSelectedRow<T>()
{
if (this.CurrentRowIndex == -1)
{
return default(T);
}
return (base.DataSource as BindingList<T>)[this.CurrentRowIndex];
}
But it is binded to only one Main grid , i also want to use this property to another Grids
I dont want data of a particular column , i want entire row data .. and dont want any iteration to be perform ...
Is there any single liner operation for this ?
Please suggest if I am missing any links
Since you are only showing 2 lines of code, I am really not sure exactly what is going on. First off, I get you want the entire row when you click some button. However, you never state how the current row is being selected.
In order to get the full selected row, you must have a selected cell. The MSDN documentation says this on the page for DataGridView.SelectedRow. So I am assuming that the user will click a cell, and you want the entire row. I would create a variable that will hold your selected row. Then when the user clicks the cell, automatically select the row and save it. Then when the button is clicked, just retrieve the the already saved row.
private DataGridViewRow selectedRow { get; set; }
Then have the event for when the user clicks a cell
private void grdGLSearch_CellClick(object sender, DataGridViewCellEventArgs e)
{
selectedRow = grdGLSearch.Rows[e.RowIndex];
}
Finally, the button click event
private void SubmitBtn_ItemClick(object sender, ItemClickEventArgs e)
{
// to target the specific data
var cellVal1 = selectedRow.Cells["SpecificCell1"].Value;
var cellVal2 = selectedRow.Cells["SpecificCell2"].Value;
}

C# anyone see why this doesn't correctly select row in a datagridview

i have a datagridview and i would like the rowheader to correctly select that entire row.
Although i thought it should anyway, it does not. i have tried the following but with no luck, can you see something obvious? =P
regards, Dave
private void dataGridView2_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
dataGridView2.Rows[e.RowIndex].Selected = true;
}
Try setting the
DataGridView.MultiSelect=false;
and
DataGridView.SelectionMode = FullRowSelect;
You can read about the MultiSelect Property and SelectionMode Property in the MSDN library linked.
If you want the user to select multiple rows, then set MultiSelect to true.
DataGridView.MultiSelect=true;
EDIT
And then you can call your event like this:
private void dataGridView2_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
dataGridView2.Rows[e.RowIndex].Selected = true;
}
To select individual cells within the data grid view and select the entire row on row header click, set the selection mode to RowHeaderSelect
DataGridView.SelectionMode = RowHeaderSelect;
The MSDN explanation for RowHeaderSelect is: Clicking a cell selects it. Clicking a row header selects the entire row.

Categories