XtraGrid whole row is highlighted except the clicked cell - c#

When I select a row in the following GridView, the cell that my mouse is resting on (in other words the cell that I left click on) to select a row, is not highlighted while the rest of the row's cells are all highlighted.
I would appreciate your help.
GridView myView = (GridView)oGrid.MainView;
myView.OptionsSelection.MultiSelect = true;
myView.OptionsSelection.MultiSelectMode = GridMultiSelectMode.RowSelect;
if (myView.RowCount > 0)
{
frmChangeMyStatus ff = new frmChangeMyStatus(ccfrms);
DialogResult dr = ff.ShowDialog();
if (dr == DialogResult.OK)
{
for (int i = 0; i < myView.SelectedRowsCount; i++)
{
row = myView.GetSelectedRows()[i];
//...........
}
}
}

If you want the focused cell to look like any other cell in the focused row, disable focused cell styling in view properties. You can do this in two different ways:
At runtime:
myView.OptionsSelection.EnableAppearanceFocusedCell = false;
At design-time: Invoke XtraGrid designer, select Views :: (your view) :: OptionsSelection :: Set EnableAppearanceFocusedCell to False.
If you have access to XtraGrid designer, you can check out the Appearance section if you need more complicated styling rules.

In addition to what Yuriy Guts said above about the focus cell appearance for the view, if the cell that is selected is editable, it will still not highlight that cell.
So, if the cell doesn't need to be editable, you can set OptionsColumn.AllowEdit = false for that column. Otherwise, if the user selects a row by clicking on a cell, you have to live with that appearance so that the user can tell which cell they are currently editing.

Related

Why will not Grid CurrentRow.index include Grid Line Selection?

If you can, please help me!
I made a line in a grid list. The selection is made apparently. The problem is that this selection does not change the CurrentRow.index. The CurrentRow.Index value was 0 before the selection. There is still 0 left after the selection :-(. How can I solve the selection and the CurrentRow index add the same value as I did?
int row_again = 3;
DataGridView_CONNECT.Rows[row_again].Selected = true;
int gridview_pointer = DataGridView_CONNECT.CurrentRow.Index;
//gridview_pointer = 0(?!)
dataGridView.Rows[index].Selected actually does not selects a row. It sets a value indicating whether the row is selected.
If you would like to select a row programmatically, first, select a cell in that row:
DataGridView_CONNECT.CurrentCell = DataGridView_CONNECT.Rows[row_again].Cells[0];
Then you can update the information that row is selected and access to the CurrentRow.Index.
DataGridView_CONNECT.Rows[row_again].Selected = true;
int gridview_pointer = DataGridView_CONNECT.CurrentRow.Index;

Load data from gridview into a textbox

I want to load data from a row that when selected by means of a button loads them into a format. For this I am doing with the CommandName, as if they were coordinates, clicking on such a row will load this data and so on for all. But I do not know how to do it in rows and cells with textbox.
I did so like it's in the code but I do not know if it's fine.
Where you have two dashed lines is how I wrote it for the Textbox.
In the form (format) I have Textbox and there should automatically appear the data when selecting any row.
if (e.CommandName.ToString() == "clic") {
//DETERMINING THE INDEX OF THE SELECTED ROW
int indexrow = int.Parse(e.CommandArgument.ToString());
int id = (int)this.gridview.DataKeys[indexrow]["Id"];
-- TextBox1.Text = gridview.Rows[indexrow].Cells[2].ToString();
This is how you accomplish what you want (I think). But in your question I don't see a TextBox present in the GridView.
if (e.CommandName.ToString() == "clic")
{
//cast the container of the sender back to a gridviewrow
GridViewRow row = (GridViewRow)((Control)e.CommandSource).NamingContainer;
//get the row number
int indexrow = row.RowIndex;
//get the value of cell 2 in the textbox
TextBox1.Text = gridview.Rows[indexrow].Cells[2].Text;
}
If you mean to use "Edit" mode in the GridView with EditItemTemplate, then have a look at this tutorial.

Scroll goes bottom, but content keeps at top

I am working with an own extended System.Windows.Forms.Datagrid... problem is that when rows are appended, the control does not scroll bottom correctly.
Here is the snippet I use:
if (filasAInsertar.Length > 0)
{
int row_count = niceDataGridDesvios.getVisibleRowsCount(niceDataGridDesvios.Parent) - 1;
ExtendedDataGrid extendedDataGrid = niceDataGridDesvios.dataGrid;
extendedDataGrid.getScrollBar().Value = extendedDataGrid.getScrollBar().Maximum;
niceDataGridDesvios.dataGrid.selectFullRow(row_count);
}
This code makes the scrollbar run bottom, but content keeps on top.... Any idea on how to make it well? Already tried to .performLayout() and .Refresh(), got same results.
Hope you guys could help me
DataGrid
To set the current row of a System.WindowsForms.DataGrid and scroll to the row you can use CurrentRowIndex property:
datGrid1.CurrentRowIndex = 50;
For example to scroll to the last row:
datGrid1.CurrentRowIndex = d.BindingContext[datGrid1.DataSource].Count - 1;
DataGridView
CurrentCell
If you set the CurrentCell of DataGridView it selects the specified cell and scrolls to make the cell visible.
For example to select the last row and scroll to it:
dataGridView1.CurrentCell = dataGridView1.Rows[this.dataGridView1.RowCount - 1].Cells[0];
FirstDisplayedScrollingRowIndex
You can also set FirstDisplayedScrollingRowIndex to scroll to a specific row, but it doesn't select the row:
For example to only scroll to the last row:
dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.RowCount-1;

DataGridView backcolor of the first row is white although the selection backcolor is transparent

I would like to hide the selection option of my DataGridView so that it seems always like nothing has been selected.
I have set the SelectionBackColor property of my DataGridView to Transparent. But when it loads, the first row's back color is always white although it turns transparent as I select other rows. But at the beginning it is always white.
This is how it looks after loading:
And this is how it looks as I click on another row:
How can I make it so that it always looks like the second picture?
Just deselect the first row after you've filled the grid. By default the first row is selected when you fill it, but it is possible to have no rows at all selected.
DataTable dtb = new DataTable("D");
dtb.Columns.Add("C1");
dtb.Rows.Add("A");
dtb.Rows.Add("B");
dtb.Rows.Add("C");
dtb.Rows.Add("D");
dtb.Rows.Add("E");
dataGridView1.DataSource = dtb;
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
if (dataGridView1.SelectedRows.Count > 0)
{
dataGridView1.Rows[0].Selected = false;
}

DGV ComboBox Cell remain updateable

I have a DataGridView that I want users to be able to add records to directly. This is done by clicking a link beneath the DGV, at which point a new row is programmatically added, and the first visible cell is of ComboBoxCell type. Code excerpt for how I'm doing this is:
// Add a new row to DGV
DataGridView dgv = this.dgvInformation;
DataGridViewRow newRow = new DataGridViewRow();
dgv.Rows.Add(newRow);
// Create cells and add to row
DataGridViewComboBoxCell cellInfoType = new DataGridViewComboBoxCell();
newRow.Cells["InfoType"] = cellInfoType;
// Create DataSource based off LINQ query here
List<ComboItemAccountInfoType> comboDataSource = new List<ComboItemAccountInfoType>();
// List is populated here
// Assign DataSource to combo cell
cellInfoType.DataSource = comboDataSource;
cellInfoType.ValueMember = "AccInfoTypeID";
cellInfoType.DisplayMember = "InfoType";
// Scroll new row into view and begin editing
dgv.FirstDisplayedScrollingRowIndex = dgv.Rows.Count - 1;
dgv.CurrentCell = dgv[1, dgv.Rows.Count - 1];
dgv.BeginEdit(true);
The DataSource contains some values with an ID of -1, which are categories that the user should not be able to select, and all other values have their valid database ID. This all works fine, except that if a user has selected a -1 row I am unable to keep the combo cell in edit more, as it simply stores the value in the cell and I can no longer activate the drop-down. I have added the following code to the _CellValueChanged event, but it has no effect:
private void dgvInformation_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
DataGridView dgv = this.dgvInformation;
DataGridViewCell cell = dgv[e.ColumnIndex, e.RowIndex];
// If type of cell is ComboBox then
if (cell.GetType() == typeof(DataGridViewComboBoxCell))
{
DataGridViewComboBoxCell cellInfoType = (DataGridViewComboBoxCell)cell;
if ((int)cellInfoType.Value == -1)
{
MessageBox.Show("Going back into edit mode...");
dgv.CurrentCell = cell;
dgv.BeginEdit(true);
}
else
{
MessageBox.Show(cellInfoType.Value.ToString());
}
}
}
I do get the "Going back into edit mode..." message here after moving onto another cell, but it doesn't then do what it says! Could anyone please explain why it won't go back into edit mode, or is there a way of preventing the value becoming locked as soon as it has been selected?
Many thanks!
There should be a "validating" event for you to intercept and cancel if the value is -1.

Categories