How do I set a Winforms DataGridView's rows header cell value when in virtual mode? - winforms

I am trying edit the header cell value when using a grid view in virtual mode, but I can't get it working.
In my CellValueNeeded event I have the following code:
grvResults.Rows[e.RowIndex].HeaderCell.Value = record.RecordNumber;
Unfortunately, my header cells are still displaying blank.
How can I get the row header values to display? The end result is I need my rows to show what row number they are, to better keep track of data.
Update: The full event code is below:
private void grvResults_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
{
const int BATCH_REQUEST_SIZE = 50;
int row = e.RowIndex + 1;
LogRecord record = null;
// Check if this record for this row is cached
if (!_recordCache.ContainsKey(row))
{
// Retrieve the requested record + a batch more to speed up operations and lessen queries
int page = (row / BATCH_REQUEST_SIZE) + 1;
var records = _currentStore.GetFilteredRecords(_filters, _currentSessionId, BATCH_REQUEST_SIZE, page);
if (records.Count > 0)
{
// Add all the records to the cache
for (int x = 0; x < records.Count; x++)
if (!_recordCache.ContainsKey(row + x))
_recordCache.Add(row + x, records[x]);
}
}
// Get the record from the cache
record = _recordCache[row];
if (record != null)
{
// Set the header cell to the record number
grvResults.Rows[e.RowIndex].HeaderCell.Value = record.RecordNumber;
// Match the cell to the field
string cellFieldName = grvResults.Columns[e.ColumnIndex].Name;
if (record.Fields.Any(x => x.FieldName.Equals(cellFieldName, StringComparison.CurrentCultureIgnoreCase)))
e.Value = record.Fields.Where(x => x.FieldName.Equals(cellFieldName, StringComparison.CurrentCultureIgnoreCase)).Single().StringValue;
}
}

In the brief experiments I've done the HeaderCell.Value always shows in the row header, regardless of where it was set or whether the grid was in virtual mode.
One possibility is that the row header is too narrow to see the value.
Another is that RecordNumber is not a string - the Value displayed in the row header must be a string.

Same thing was happening to me, I noticed I was applying the header to the new row and then overwriting it with a row with values but with no header, so just try this:
grvResults.Rows[e.RowIndex-1].HeaderCell.Value = record.RecordNumber;
(rowindex -1)

Related

Winform dataGridView cells proble

I have a DataGridView in a Winform desktop project in net 6.0
This DataGridView's generic row is composed by a DataGridViewCheckBoxCell in the first column, and another 6 DataGridViewTextBoxCells in the subsequent columns : the first one of the DataGridViewTextBOxCell (column 1) is permanently ReadOnly.
What I want to do is make the Cells from the second TextBox to the last one (that is columns 2 to 6) ReadOnly when the CheckBox is unchecked, and restore them to ReadWrite when the checkbox is checked.
What I do is override the dataGridViev.CellClick event, and when the sender is the element in column 0 (the checkbox) I get the row index and I iterate on the cells of the row from index 2 to 6 and set the ReadOnly property to true or false depending on the state of the checkobx itself.
The mechanism basically works fine, I read the event on the exact column/row, I correctly get the state of the checkobox (maybe in a not-so-smart way...) : but when it comes to setting the ReadOnly property I can see that ,in spite of iterating on the 2 to 6 column, it skips the cell in column 2 and sets the one in column 1 instead: all the settings on the other columns work correctly. I thought it was a matter of configuring the columns in the designer but all the columns from 2 to 6 are set identically.
My event handler is the following :
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0) // if checkbox
{
int row = e.RowIndex;
bool setState = true;
DataGridViewCheckBoxCell check = (DataGridViewCheckBoxCell)(dataGridView1.Rows[row].Cells[0]);
if (check.Value != null && check.Value == "true")
{
setState = true;
check.Value = "false";// CheckState.Unchecked;
}
else
{
setState = false;
check.Value = "true";// CheckState.Checked;
}
for (int i = 2; i < dataGridView1.Rows[row].Cells.Count; i++)
{
DataGridViewTextBoxCell txt = (DataGridViewTextBoxCell)(dataGridView1.Rows[row].Cells[i]);
txt.ReadOnly = setState;
if (setState) txt.Style.BackColor = Color.Gray;
else txt.Style.BackColor = Color.White;
}
}
}

how to avoid whitespace on right side if columns are less in Ag grid

The answer might be sizeTofit .
But When I have to resize the column then that whitespace is coming on the right side of the grid.
So I need to do the last column of the grid to occupy the grid fully if columns are less (Or if I remove come column)otherwise it will take the column width provided by the property to Ag grid
This is the image I am getting now
like this I want
if size of it is not what you want then you can write a function and do the calculations manually.
set the width of columns and at the end set new column defs to your ag grid;
var tableWidth = sth;
var totalWidth = 0;
for(var i = 0; i<columnDefs.length; i++) {
totalWidth += columnDefs[i].width;
if((i == columnDefs.length - 1) && (totalWidth < tableWidth)) {
columnDefs[i] += tableWidth - totalWidth;
}
}
api.setColumnDefs(columnDefs);

How to get the selected column count in datagrid

I want selected column's index of the DataGrid. For example, if I select the first column, I want the first column's index (index = 0).
I tried it in the DataGrid SelectionChanged event, but I can't seem to get the particular column index. If any one knows how to do it, help me with some sample code.
The DataGrid.Items property returns a DataGridItemCollection representing the DataGridItems in the DataGrid.
Each DataGridItem is representative of a single row in the rendered table. Also, the DataGridItem exposes a Cells property which represents the no. of tablecells (in other words, the columns) in the rendered table.
// Get the Row Count
int rowCount = myGrid.Items.Count;
// Get the no. of columns in the first row.
int colCount = myGrid.Items[0].Cells.Count;
I am assuming that you want the indexes of any columns that are selected. Here is the code I've come up with:
List<int> selectedColumnIndexes = new List<int>(dataGrid.SelectedCells.Count);
for (int i = 0; i < dataGrid.SelectedCells.Count; i++)
{
foreach (DataGridColumn column in dataGrid.Columns)
{
if (column.DisplayIndex == dataGrid.SelectedCells[i].Column.DisplayIndex)
{
if (!selectedColumnIndexes.Contains(column.DisplayIndex))
{
selectedColumnIndexes.Add(column.DisplayIndex);
}
}
}
}
Thus you will have a list of all the indexes of the columns currently selected. This question gives some nice clues in what direction to head here.
Obviously, if you want just the number of columns actually selected, then that value is simply selectedColumnIndexes.Count after the for loops have run through.

Assigning values to array of 'Combo Boxes' in a 'Group Box' using for each loop in c#

I have 10 comboBox in a groupBox
for I just want to display a calculated value in respective comboBox like this say if I set a varible double i=08.00; then on button click cmboBox should display values like this
CB1-08.00
CB2-09.50
CB3-10.00
CB4-10.50
CB5-11.00
CB6-11.50
.... and so on upto CB10 But I am getting output like this
And Code
private void button1_Click(object sender, EventArgs e)
{
double i=08.00;
foreach (var comboBox in groupBox1.Controls.OfType<ComboBox>())
{
comboBox.Text = i.ToString("00.00");
i = i + 0.5;
}
}
Your combobox order is different in the collection so it inserts the numbers randomly. May be you can name your combobox for instance like cmb1,cmb2,cmb3 etc. and if you update your code it will run.
Your controls in the Controls collection are not sorted by their appearance on the form. You will need to find a way to sort them if you need different values in each based on their position.
Foreach loop doesn't give the collection in the order you wanted. The way to go forward is to give a tag id to each combo box, then you can use that to assign a value to them them.
So your first combo box will start with tag id 0, and the last one will have 8,
double val = 08.00;
for (int i = 0; i < groupBox1.Controls.Count; ++i)
{
var combobox = groupBox1.Controls[i] as ComboBox;
int tag = int.Parse(combobox.Tag.ToString());
double value = val + (0.5 * tag);
combobox.Text = value.ToString("00.00");
}
Make sure you tag the cobbo box in the order you wanted them.

Datagridview Column width

I have problem related to datagridview in Winform.
I have a list of table names in my left panel. When I click on Table I show the table content in right panel. I am showing data in a datagridview by fetching data and assigning datasource to dgv.
I am setting following property to dgv.
dgTemp.Dock = DockStyle.Fill;
dgTemp.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
dgTemp.AutoSize = true;
dgTemp.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
dgTemp.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
dgTemp.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
dgTemp.ReadOnly = true;
dgTemp.AutoGenerateColumns = true;
dgTemp.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dgTemp.AllowUserToAddRows = false;
My problem is there can be any number of columns in Datasource I am assigning to dgv. So if there are very few number of columns (say 1 or 2) the dgv size stands very small and the empty space on right side give very ugly look.
I can't use auto autosizecolumnmode to fill since when there is more columns all columns get shrink and expanding columns dont give me Scroll at bottom
so My requirement is
All space in datagridview should be filled. (should cover all area)
When there is more column, there should appear scroll, so it give better look
are there any events or properties which I can use ??
Thanks in anticipation.
Try this :
dgTemp.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
Update :
dataGridView1.FirstDisplayedScrollingRowIndex = 5; //use 5 because you want to start from 5
//you can have a horizontal scroll bar with this code :
dataGridView1.FirstDisplayedScrollingColumnIndex = 10; //you can choose every column you wanna start with that column
Update 2 :
int rows = dataGridView1.Rows.Count;
int columns = dataGridView1.Columns.Count;
if (rows < 5 && columns < 10)
{
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
}
else
{
dataGridView1.FirstDisplayedScrollingRowIndex = 5; //use 5 because you want to start from 5
//you can have a horizontal scroll bar with this code :
dataGridView1.FirstDisplayedScrollingColumnIndex = 10; //you can choose every column you wanna start with that column
}

Resources