Data Grid View Selection c# - c#

Recently I made the 2048 game using a DataGridView. Everything works except for the blue selection/focus that appears on the DataGrid when my form starts and I use the arrow keys. I tried to remove it with ClearSelection(), works except for the arrows. How could I disable the blue selected cell? How can I disable the arrows?
public Form1_Load (......)
{
DataGridView1.ClearSelection();
}
Image Link (I need more reputation to upload it on the site)
http://s23.postimg.org/beekn9i6z/Immagine.png
Screenshot of Datagrid during Runtime
http://s1.postimg.org/s5loh0uvj/datagrid.png

Handling selection in form load is too early because form is not shown yet. Perform datagridview clear selection in form shown event.
private void Form1_Shown(object sender, EventArgs e)
{
DataGridView1.ClearSelection();
}
OR
void dataGridView2_SelectionChanged(object sender, EventArgs e)
{
dataGridView2.ClearSelection();
}

Try this
DataGridView1.CurrentCell.Selected = false;

Related

Problems with datagridview / autoresizecolumns and rows

I have a huge problems every time I try to resize my columns and rows. I'm trying to autoresize the columns and the rows with the function:
dataGridView1.AutoResizeColumns();
dataGridView1.AutoResizeRows();
If i put this two lines after I pass the datasource to dataview, it doesn't work. I tried to handle the DataSourceBindingComplete, and idem it doesn't work. I tried to set it in the form.designer.cs and it doesn't work. then
I tried to make a button
private void button1_Click(object sender, EventArgs e)
{
dataGridView1.AutoResizeColumns();
dataGridView1.AutoResizeRows();
}
and when I click the button everything works perfectly!!! It resizes all my columns and my rows. But i don't want this. I want it automatic.
Can you guys help me please and explain why it does that? Doesn't make sense, inside the original code it doesn't work, but in a separate button it works.
This has worked for me.
Set AutoSizeColumnsMode and AutoSizeRowsMode values to AllCells from None in the designer.
Have you tried setting the DataGridViewAutoSizeColumnsMode before you set the source, if possible?
private void button1_Click(object sender, EventArgs e)
{
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
dataGridView1.DataSource = SourceList; // Your Collection here
dataGridView1.AutoResizeRows();
}
I apologize that it took me more than a year to see this post...
Here's what I did to make this "automatic".
private void AutoResizeGrid()
{
if (dataGridView.Columns.Count < 1) return;
dataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.DisplayedCells);
dataGridView.AutoResizeRows(DataGridViewAutoSizeRowsMode.DisplayedCells);
}
private void MatrixDataGridView_ClientSizeChanged(object sender, EventArgs e)
{
if (!this.Visible) return;
AutoResizeGrid();
}
private void MatrixDataGridView_Scroll(object sender, ScrollEventArgs e)
{
if (!this.Visible) return;
AutoResizeGrid();
}
Before adding this code, I went to the Designer and I set "AutoSizeColumnsMode" and "AutoSizeRowsMode" both to "DisplayedCells". This causes automation when the grid first receives the data. The code above causes automation when a resize or scroll event happens.

How to detect click in GridView

I have a GridView, and in this Gridview there are several Grids which contain other elements. If I click onto one element of my GridView (one Grid) I want to open details about this element, but Grid and GridView dont seem to support setting a click method. What else can I do to call a method when an element is clicked?
but Grid and GridView dont seem to support setting a click method
Use Tapped event.
you could add an invisible rectangle or other element to register an OnMouseOver event or click ect.
Each of your Grid should subscribe to 3 events and have transparent background:
<Grid Background="Transparent" PointerPressed="Grid_OnPointerPressed" PointerReleased="Grid_OnPointerReleased" PointerExited="Grid_OnPointerExited">
in code you may simulate click event like this:
private bool _isPressed;
private void Grid_OnPointerPressed(object sender, PointerRoutedEventArgs e)
{
_isPressed = true;
}
private void Grid_OnPointerReleased(object sender, PointerRoutedEventArgs e)
{
if (_isPressed)
{
//your logic on click event
}
_isPressed = false;
}
private void Grid_OnPointerExited(object sender, PointerRoutedEventArgs e)
{
_isPressed = false;
}
or the easiest way is to wrap your Grid into Button

WinForms TabControl

I noticed that by programmatically selecting a Tab in the Tab control selects a control contained in the tab page selected.Is it possible to change this behaviour. I have a control in a tabpage that I do not want to be selected when the its tab page is selected from a button click. I have a simple form with a tab control and two tab pages. When button1 is clicked the tab page 2 is selected but so is the datagridview contained in that tab page.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.GotFocus += DataGridView1_GotFocus;
}
private void DataGridView1_GotFocus(object sender, EventArgs e)
{
//this event is called from button1_click
}
private void button1_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage2;
}
}
By default when you select a tab (or even when you start a form) the control which is the first in your tab order is automatically focused. I am assuming this is what is happening here.
You can solve this by simply unfocusing the datagridView in question. There are multiple ways to do this. Firstly you can set focus to the control that you wish to be selected instead of the dataGridView. This can be done by:
myControl.Focus = True;
Or alternatively if you want non of the controls to be selected you can set the active control to Null:
ActiveControl = NULL;
Note: ActiveControl is a property which contains the current active control.
As to where this code should be placed. That is totally dependent upon you. You can do it as soon as you change the tab in the button click event. This is what I would prefer.
I am sure there are other kludges as well to acheive the same. Hope this helps.
Here is code to select tab
private void button1_Click(object sender, EventArgs e)
{
// we can select tab by tab name
tabControl1.SelectTab("tabPage2");
tabControl1.SelectedIndex = 1;
tabControl1.TabPages[0].Hide();
tabControl1.TabPages[1].Show();
}

Simple ON/OFF toggle button with image

I'm working on a WinForms project where I'm trying to create an ON/OFF toggle button that uses two separate images (both located in project resources) for both the "ON" setting, and the "OFF" setting.
Based on what I've found online, I've used a CheckBox with its appearance set to "Button".
Here is the code I've got so far for my button:
private void ToggleButton_CheckedChanged(object sender, EventArgs e)
{
if (ToggleButton.Checked)
{
ToggleButton.BackgroundImage.Equals(Properties.Resources.ToggleButton_ON);
}
else
{
ToggleButton.BackgroundImage.Equals(Properties.Resources.ToggleButton_OFF);
}
}
For some reason nothing happens when I click on the button, and I'm not sure what I've done wrong here.
Basically, I'd like the background image to cycle back and fourth between ToggleButton_ON and ToggleButton_OFF when the user clicks on the button.
Change your code to:
private void ToggleButton_CheckedChanged(object sender, EventArgs e)
{
if (ToggleButton.Checked)
ToggleButton.BackgroundImage = Properties.Resources.ToggleButton_ON;
else
ToggleButton.BackgroundImage = Properties.Resources.ToggleButton_OFF;
}
The .Equals is for checking equality which you can override in your own classes.

c# Winforms: DataGridView leave event not getting value of last updated cell

I have a WinForms screen with a DataGridView on it and Back/Next buttons. On FormLoad, the grid is populated with values from an XML document. The user can then change the value of any cell in the grid. I've created a SaveGridValuesToXml method which updates the XML file with the updated values from the grid cells.
This mostly works okay, and I have fired the SaveGridValuesToXml method from the Leave event on the grid, so when the user clicks back or next and the grid loses focus, the save method is called.
The problem I have is that all the cell values are updated and reflected correctly in the XML file, EXCEPT for the cell which had focus when the grid loses focus. For example, in a three column grid, if I update the first, second and third cells in the first row, when the grid loses focus only the changes I made in the first and second cells are saved to my XML file.
Code examples are as follows - theLoadXmlDataIntoGridGridView() method is called when the parent form is loaded.
XmlDataDocument xmlData = new XmlDataDocument();
private void LoadXmlDataIntoGridGridView()
{
xmlData.DataSet.ReadXml(#"C:\testFile.xml");
uxMappingDataGridView.DataSource = xmlData.DataSet;
uxMappingDataGridView.DataMember = "Item";
}
private void uxBackButton_Click(object sender, EventArgs e)
{
this.Hide();
uxSettingsUserControl settingsScreen
= ParentForm.Controls["uxSettingsUserControl"] as uxSettingsUserControl;
settingsScreen.Show();
}
private void uxNextButton_Click(object sender, EventArgs e)
{
this.Hide();
uxLoadScriptUserControl loadScriptScreen
= ParentForm.Controls["uxLoadScriptUserControl"] as uxLoadScriptUserControl;
loadScriptScreen.Show();
}
private void uxMappingDataGridView_Leave(object sender, EventArgs e)
{
SaveGridValuesToXml();
}
private void SaveGridValuesToXml()
{
xmlData.DataSet.WriteXml(#"C:\testFile.xml");
}
Any suggestions?
This is due to currently editing cell not being committed when you leave the grid by clicking on a button. 1
To get that last value you need to force the changes to commit by calling EndEdit() on the grid.
In the code below I call it from SaveGridValuesToXml() - in your example during the leave handler would work also, no significant difference between the two.
private void uxMappingDataGridView_Leave(object sender, EventArgs e)
{
SaveGridValuesToXml();
}
private void SaveGridValuesToXml()
{
uxMappingDataGridView.EndEdit(DataGridViewDataErrorContexts.Commit);
xmlData.DataSet.WriteXml(#"C:\testFile.xml");
}
1 Not entirely sure why it works this way - personally feels like a bug but maybe someone on the DataGridView product team would say it was a feature.

Categories