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.
Related
iam looking for a method to set the color specific only for 1 cell by clicking on it.
I build a Canteen Voter, which you can choice between 4 menues each day.
My DataGridView is connected to a database from which it receives all information and when you click on a cell it is also stored in the database for the logged-in user.
Now I would like to be able to color the cells individually when clicking.
So far I only found the SelectedCells property, but this actually only sets a color for "SelectedCells" when these cells lose focus again they are white again.
I would have liked them to be able to keep this color to make it even clearer to the user which dish he had just chosen.
You should set DataGridViewCell.Style property.
Take a look at this example
private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex < 0 || e.RowIndex == dataGridView1.NewRowIndex)
{
return;
}
var dataGridViewCellStyle =
new DataGridViewCellStyle(dataGridView1.DefaultCellStyle)
{
BackColor = Color.Gold
};
dataGridView1[e.ColumnIndex, e.RowIndex].Style = dataGridViewCellStyle;
}
I have created a Data Grid View in Visual studio using c#. It has three columns. Now I want to add a new column at the end containing list data. Column with one row of size of the Data Grid View table. Grayed one in the image I have created yellow part I have to add.
You could to do not draw a Top border for that column. Something like this:
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
e.AdvancedBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.None;
if (e.ColumnIndex == 3 && e.RowIndex > 1)
{
e.AdvancedBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.None;
}
}
I have a desktop app and I am using DataGridView on form. I froze the first row. But when I clicked the header of column in DataGridView The location of the first row is also changing. How to ignore the first row while I am sorting.
Within the SortCompare event handler for the SortCompare event of the DataGridView, try this:
private void dataGridView1_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
{
if (e.RowIndex1 == 0)
{
e.Handled = true;
}
else
{
e.Handled = false;
}
}
Essentially, what you're asking is to be able to sort only a portion of the DataGridView. To my knowledge, there's no easy or standard way to do this, echoed by Filip's response to this question: Sorting selected rows in DataGridView. This doesn't mean it's impossible to achieve the behavior that you want.
Filip offers an implementation, and in this question, Justin offers code that you would have to modify slightly, but that may suit your needs. The question asks how to move a row up or down in a DataGridView, but I think you could modify his implementation to move the row you want to keep static all the way to the top after you sort. Depending on the volume of rows you're sorting, the first row might disappear and reappear. However, if your data set is relatively small, I can't imagine the brief flash of the first row being sorted to some random position and then added back to the top would cause any major problems.
private void dataGridView1_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
{
int so = 1;
if (dataGridView1.SortOrder == SortOrder.Ascending)
{
so = -1;
}
if (e.RowIndex1 == 0 )
{
e.SortResult = so;
e.Handled = true;
}
if (e.RowIndex2 == 0)
{
e.SortResult = -so;
e.Handled = true;
}
}
This code puts the first line always on top of the sorted list, by setting the sort result of the first row accordingly.
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];
}
}
i have a Datagridview that contains columns.
when i click on the item on the column.
it always select the first row
i also tried
.currentcell = null and .clearselection();
// i inserted this one on the form_load
but nothing happens.
here is my code
private void dgv1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0)
{
dgv1.CurrentCell = null;
dgv1[e.ColumnIndex, e.RowIndex].Value.ToString();
if (Path.GetExtension(path) == ".pdf")
{
Process.Start(path);
}
}
}
What do you want the user to select in the first place?
There is a property for selectionmode in datagridview.
Change it to CellSelect if you want it to select cells only.
Alternatively, use dataGridView.SelectionMode = DataGridViewSelectionMode.CellSelect;
After reading what you said on the comment, from that I infer that you are trying to click a cell and read the cell from your program?
If so, you could try
dataGridView[e.ColumnIndex, e.RowIndex].Value.ToString();
on the CellContentClick and/or CellClick event