This area of DataGridView called as? - c#

What is name of following area in DataGridView? Can I do something with this area (like changing caption)? Is there are events for this area?

That portion is called as TopLeftHeaderCell
Even datagridview provides you a property named TopLeftHeaderCell
You can change the content of that cell via following code
dataGridView1.TopLeftHeaderCell.Value = "abc";
To capture click event of that cell capture CellClick event and handle e.ColumnIndex = -1 and e.RowIndex = -1
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == -1 && e.RowIndex == -1)
{
//TopLeftHeaderCell clicked
}
}
If you want to use the RowHeaderCell try this
dataGridView1.Rows[0].HeaderCell.Value = "1";
this will allow you to show numbers like Row Number

Related

Infragistics Get Name-Property trough Cell Click c#

I have created more than 5 GRID for which there are a few column that are empty/zero. These are numbered as example 1,2,3...
I have already implemented that I can select and deselect single cells for this columns.
Now my question is (and I am still very inexperienced with Infragistics)
How do I get the Name Property of my grid just by clicking on it?
Some parameters I could read out like the cells.index or the rows.index, but I want to know for another method the exact Name Property in which just the cell was clicked.
Via the event handler I let all my grids into the cells click method
private void GRD_LIST_Grid_ClickCell(object sender, Infragistics.Win.UltraWinGrid.ClickCellEventArgs e)
{
if (e.Cell.Column.ToString().Contains("BAHN") && e.Cell.Tag == "1") //1-5 Column have the name "BAHN", the Tag is for an flag if the cell is clicked before
{
e.Cell.Selected = false;
e.Cell.Tag = null;
SetPrio(e.Cell.Row.Index, e.Cell.Column.Index);
}
else if (e.Cell.Column.ToString().Contains("BAHN") && e.Cell.Tag == null)
{
SetPrio(e.Cell.Row.Index, e.Cell.Column.Index);
e.Cell.Tag = "1";
}
//when the cell is click show the cell in green
if (e.Cell.Tag == "1")
{
e.Cell.Row.Cells[e.Cell.Column.ToString()].Appearance.BackColor = System.Drawing.Color.Green;
}
else if (e.Cell.Tag == null)
{
e.Cell.Row.Cells[e.Cell.Column.ToString()].Appearance.BackColor = System.Drawing.Color.FromArgb(234,244,243);
}
ClearSelectedRow(); //a Method where i set all grids in an array and clear all rows because i dont know which grid i am
}
When the ClickCell event occurs the first parameter of the event handler is sender and in this case it's the Infragistics.Win.UltraWinGrid.UltraGrid source. Therefore, it is possible to get the grid name like in code below:
private void GRD_LIST_Grid_ClickCell(object sender, Infragistics.Win.UltraWinGrid.ClickCellEventArgs e)
{
if (sender is Infragistics.Win.UltraWinGrid.UltraGrid ugrid)
{
System.Diagnostics.Debug.WriteLine($"The ClickCell event is raised by '{ugrid.Name}'");
}
}

DataGridView Remove GridLines from first column

I'm attempting to remove the gridlines on the first column in a datagridview so it looks like this:
Unfortunately i've only managed to remove the lines on a row/cell basis but what i need is to remove the lines for the entire column but keep the right hand side line, is there a standard way of acheiving this?
Following code must do what you want
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == 0 && e.RowIndex > -1)
{
e.AdvancedBorderStyle.Left = DataGridViewAdvancedCellBorderStyle.None;
e.AdvancedBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.None;
e.AdvancedBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.None;
}
}
One dirty-hack would be to recursively change cell styles on Datagridview Paint Event under the particular column. Use a for-loop to manipulate the cell border style.

Change datagridview cell value in edit mode

I have a cell in datagridview in which I display time in a custom format. I need when used enters edit mode (for example by double-click), I need to change the string value to integer representing the time in minutes.
When I try to change the cell value in "CellEnter" event, it doesn't seem to respond. Actually it doesn't seem to change the cell value pretty much inside any event.
Please don't mind the details of converting time to string and vise versa, my question is how can I successfully change the content of a cell when user double-clicks on it.
Edit (code + solution):
What I did is use another column to store the actual value (without formatting). On cell formatting of that column I'm passing the value to custom format function to fill my column.
private void gridview_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
if (e.ColumnIndex == 3 && e.Value != null && e.Value.ToString() != "")
{
//fill the unbound textbox column (5) from raw value column (3)
string newValue = TimeAttendanceHelper.FormatHourlyDuration(e.Value);
gridview.Rows[e.RowIndex].Cells[5].Value = newValue;
}
}
And then thanks to TaW, on CellBeginEdit I am showing the raw value to edit it:
private void gridview_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
if (e.ColumnIndex == 5)
{
//on editing, use the value from raw data column (3)
gridview.Rows[e.RowIndex].Cells[5].Value = gridview.Rows[e.RowIndex].Cells[3].Value;
}
}
And Finally when CellEndEdit, I reformat the new value:
private void gridview_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 4)
{
//update value both in columns 3 & 5
string newValue = tIME_SHIFTDataGridView.Rows[e.RowIndex].Cells[4].Value.ToString();
gridview.Rows[e.RowIndex].Cells[3].Value = newValue;
gridview.Rows[e.RowIndex].Cells[4].Value = TimeAttendanceHelper.FormatHourlyDuration(newValue);
}
}
When the cell is in edit mode you need to change the text in the edit control, usually a Textbox. You can get (and hold) a handle to it in the EditingControlShowing event:
TextBox editBox = null;
private void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is TextBox) editBox = e.Control as TextBox;
}
But using the CellEnter event is not a good idea as it will be called when scrolling or clicking around as well..
To catch the beginning of editing you use the BeginEdit event:
int yourEditColumn = 5;
private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
if (e.ColumnIndex == yourEditColumn )
{
string yourValue = "12345";
dataGridView1.Rows[e.RowIndex].Cells[yourEditColumn ].Value = yourValue;
if (editBox != null) editBox.Text = yourValue;
}
}

DataGridView equivalent of Listbox's IndexFromPoint

Is there an DataGridView equivalent of ListBox's IndexFromPoint method? I need it so that I can select a given cell when it is right-clicked, which it doesn't seem to detect normally, though left-clicks do select the cell. When I was using a ListBox, I achieved this through use of the IndexFromPoint method, which is why I bring it up here.
Try using the CellContentClick event. Make sure you verify the RowIndex is greater than 0 to handle when a user right clicks on a column header.
private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right && e.RowIndex >= 0)
{
dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
}
}

How to make particular cell visible inside Gridview, not complete column?

I had a Gridview which contains 3 columns , initially during form load i only two columns will be visible one column will be having data and other having checkbox.
i want when i check a checkbox in particular cell, corresponding to that checkbox the third column cell will be visible, i don't want to complete 3rd column to get visible on checking a check box ,Gridview is hardcoded only rows are dymanic (column1,column2 are set to visible and column 3 is set to invisible)
in below inmage when i m checking checkbox complete thried column is visible,which i don't want
can any one help me in this?
i tried below code but it is making 3rd column visible not, particular cell
public form1()
{
dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
}
void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
dataGridView1.Columns[e.ColumnIndex + 1].Visible = true;
}
Well You can try RowCommand event for this but as i see you already have event created for checkbox ,Try to find row Index for particular row and then use Cell number (cell[2]) to find the control and assign its property as visible = false,
Here is the demo I've just tried. It seems to work OK. The whole idea is You can't hide a particular cell in DataGridView. However you can make it hidden as a normal GUI engine will use when it wants to hide any control/element (I think so). You just customize it to paint the cell with the BackgroundColor of your DataGridView. Of course, to make it work, it's not such easy. Here is the code for you:
//First, you have to be sure the whole third column is Visible.
//CellPainting event handler for your dataGridView1
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == 2)//This is the Column index you want to hide.
{
object o = e.RowIndex == -1 ? null : dataGridView1[e.ColumnIndex - 1,e.RowIndex].Value;
if (o!=null &&!(bool)o || e.RowIndex == -1 || e.RowIndex == dataGridView1.RowCount - 1)
{
e.Graphics.FillRectangle(new SolidBrush(dataGridView1.BackgroundColor), e.CellBounds);
if(e.RowIndex > -1) dataGridView1[e.ColumnIndex, e.RowIndex].ReadOnly = true;
e.Handled = true;
}
if (o != null && (bool)o)
{
dataGridView1[e.ColumnIndex, e.RowIndex].ReadOnly = false;
}
}
}
//CellContentClick event handler for your dataGridView1
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
UpdateThirdColumCell(e);
}
//CellContentDoubleClick event handler for your dataGridView1
private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
{
UpdateThirdColumCell(e);
}
private void UpdateThirdColumCell(DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 1)//The column index of the CheckBox column
{
DataGridViewCheckBoxCell cell = (DataGridViewCheckBoxCell)dataGridView1[e.ColumnIndex, e.RowIndex];
cell.Value = cell.EditingCellFormattedValue;
dataGridView1.Invalidate();
if ((bool)cell.Value)
{
dataGridView1.CurrentCell = dataGridView1[e.ColumnIndex + 1, e.RowIndex];
}
}
}
//CellStateChanged event handler for your dataGridView1
private void dataGridView1_CellStateChanged(object sender, DataGridViewCellStateChangedEventArgs e)
{
if (e.Cell.ColumnIndex == 2 && e.Cell.Selected)
{
dataGridView1.BeginEdit(false);
}
}
And that's all :)
Yeah ,actually we can't make invisible but i want to give an explianation about it in simple way.using
datagridview.rows(e.rowindex).cells[your column name]=true ;
using this it won't allow the user to enter the data in the textbox.if it is false then we can modify it/enter any text.
datagridview.rows(e.rowindex).cells[your column name].backcolour=color.gray/white/black;
using this we can fill color to a particular textbox which is to be painted when checkbox is true/false;

Categories