private void dataGridView_CellValidating(DataGridView dataGridView, DataGridViewCellValidatingEventArgs e) { if (dataGridView.Rows[e.RowIndex].IsNewRow) return; if (dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].ReadOnly) return; if (e.ColumnIndex == 1) { if (e.FormattedValue.ToString() == StarQualityList[12]) dataGridView.CancelEdit(); } if (e.ColumnIndex == 2) { int quality; bool isValidQuality = Int32.TryParse(e.FormattedValue.ToString(), out quality); if (!isValidQuality || quality < -1 || quality > Item.MaxQuality) { dataGridView.Rows[e.RowIndex].Cells[2].ErrorText = "Quality value must be a valid number between -1 and 300"; dataGridView.CancelEdit(); } else dataGridView.Rows[e.RowIndex].Cells[2].ErrorText = null; } if (e.ColumnIndex == 3) { int quantity; bool isValidQuantity = Int32.TryParse(e.FormattedValue.ToString(), out quantity); if (!isValidQuantity || quantity < 1 || quantity > 99) { dataGridView.Rows[e.RowIndex].Cells[3].ErrorText = "Amount must be a valid number between 1 and 99"; dataGridView.CancelEdit(); } else dataGridView.Rows[e.RowIndex].Cells[3].ErrorText = null; } }
private void spillBag(DataGridView dgv, int bag) { var pouch = Pouches[bag]; var itemcount = Pouches[bag].Items.Length; string[] itemarr = Main.HaX ? (string[])itemlist.Clone() : getItems(pouch.LegalItems); dgv.Rows.Clear(); dgv.Columns.Clear(); DataGridViewComboBoxColumn dgvItemVal = new DataGridViewComboBoxColumn { DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing, DisplayIndex = 0, Width = 135, FlatStyle = FlatStyle.Flat }; DataGridViewColumn dgvIndex = new DataGridViewTextBoxColumn(); { dgvIndex.HeaderText = "CNT"; dgvIndex.DisplayIndex = 1; dgvIndex.Width = 45; dgvIndex.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; } foreach (string t in itemarr) dgvItemVal.Items.Add(t); // add only the Item Names dgv.Columns.Add(dgvItemVal); dgv.Columns.Add(dgvIndex); dgv.Rows.Add(itemcount > 0 ? itemcount : itemarr.Length); dgv.CancelEdit(); string itemname = ""; string err = ""; for (int i = 0; i < pouch.Items.Length; i++) { int itemvalue = pouch.Items[i].Index; if (itemvalue >= itemlist.Length) { Util.Error("Unknown item detected.", "Item ID: " + itemvalue, "Item is after: " + itemname); dgv.Rows[i].Cells[0].Value = itemarr[0]; dgv.Rows[i].Cells[1].Value = 0; continue; } itemname = itemlist[itemvalue]; int itemarrayval = Array.IndexOf(itemarr, itemname); if (itemarrayval == -1) { dgv.Rows[i].Cells[0].Value = itemarr[0]; dgv.Rows[i].Cells[1].Value = 0; err += itemname + ", "; } else { dgv.Rows[i].Cells[0].Value = itemname; dgv.Rows[i].Cells[1].Value = pouch.Items[i].Count; } } if (err.Length > 0) Util.Alert($"The following items have been removed from {Pouches[bag].Type} pouch.", err, "If you save changes, the item(s) will no longer be in the pouch."); }
[Test] // Xamarin bug 5419 public void EditingControlShowingTest_Bound () { using (DataGridView _dataGridView = new DataGridView ()) { DataGridViewComboBoxColumn _nameComboBoxColumn; DataGridViewTextBoxColumn _firstNameTextBoxColumn; DataGridViewCheckBoxColumn _chosenCheckBoxColumn; _dataGridView.AutoGenerateColumns = false; // Add the event-handler. _dataGridView.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler (DataGridView_EditingControlShowingTest); // No columns have been found in the event-handler yet. editingControlShowingTest_FoundColumns = 0; // _nameComboBoxColumn _nameComboBoxColumn = new DataGridViewComboBoxColumn (); _nameComboBoxColumn.HeaderText = "Name"; _nameComboBoxColumn.DataPropertyName = "Name"; _dataGridView.Columns.Add (_nameComboBoxColumn); // _firstNameTextBoxColumn _firstNameTextBoxColumn = new DataGridViewTextBoxColumn (); _firstNameTextBoxColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; _firstNameTextBoxColumn.HeaderText = "First Name"; _firstNameTextBoxColumn.DataPropertyName = "FirstName"; _dataGridView.Columns.Add (_firstNameTextBoxColumn); // _chosenCheckBoxColumn _chosenCheckBoxColumn = new DataGridViewCheckBoxColumn (); _chosenCheckBoxColumn.HeaderText = "Chosen"; _chosenCheckBoxColumn.DataPropertyName = "Chosen"; _chosenCheckBoxColumn.FalseValue = "false"; _chosenCheckBoxColumn.TrueValue = "true"; _dataGridView.Columns.Add (_chosenCheckBoxColumn); // .NET requires that all possible values for combo-boxes in a column // are added to the column. _nameComboBoxColumn.Items.Add ("de Icaza"); _nameComboBoxColumn.Items.Add ("Toshok"); _nameComboBoxColumn.Items.Add ("Harper"); _nameComboBoxColumn.Items.Add ("Boswell"); // Set up the contents of the data-grid. BindingList<EcstRecord> boundData = new BindingList<EcstRecord> (); boundData.Add (new EcstRecord ("de Icaza", "Miguel", true)); boundData.Add (new EcstRecord ("Toshok", "Chris", false)); boundData.Add (new EcstRecord ("Harper", "Jackson", false)); boundData.Add (new EcstRecord ("Boswell", "Steven", true)); _dataGridView.DataSource = boundData; // For data binding to work, there needs to be a Form, apparently. Form form = new Form (); form.ShowInTaskbar = false; form.Controls.Add (_dataGridView); form.Show (); // Make sure the data-source took. // (Without the Form, instead of having four rows, the data grid // only has one row, and all its cell values are null.) Assert.AreEqual (boundData.Count, _dataGridView.Rows.Count, "1-6"); // Edit a combo-box cell. _dataGridView.CurrentCell = _dataGridView.Rows[3].Cells[0]; Assert.AreEqual (true, _dataGridView.Rows[3].Cells[0].Selected, "1-7"); Assert.AreEqual (true, _dataGridView.BeginEdit (false), "1-8"); _dataGridView.CancelEdit(); // Edit a text-box cell. _dataGridView.CurrentCell = _dataGridView.Rows[0].Cells[1]; Assert.AreEqual (false, _dataGridView.Rows[3].Cells[0].Selected, "1-9"); Assert.AreEqual (true, _dataGridView.Rows[0].Cells[1].Selected, "1-10"); Assert.AreEqual (true, _dataGridView.BeginEdit (false), "1-11"); _dataGridView.CancelEdit(); // Edit a check-box cell. _dataGridView.CurrentCell = _dataGridView.Rows[3].Cells[2]; Assert.AreEqual (false, _dataGridView.Rows[0].Cells[1].Selected, "1-12"); Assert.AreEqual (true, _dataGridView.Rows[3].Cells[2].Selected, "1-13"); Assert.AreEqual (true, _dataGridView.BeginEdit (false), "1-14"); _dataGridView.CancelEdit(); // Make sure the event-handler was called each time. // (DataGridViewCheckBoxCell isn't derived from Control, so the // EditingControlShowing event doesn't get called for it.) Assert.AreEqual (3, editingControlShowingTest_FoundColumns, "1-14"); // Get rid of the form. form.Close(); } }
[Test] // Xamarin bug 5419 public void EditingControlShowingTest_Unbound () { using (DataGridView _dataGridView = new DataGridView ()) { DataGridViewComboBoxColumn _nameComboBoxColumn; DataGridViewTextBoxColumn _firstNameTextBoxColumn; DataGridViewCheckBoxColumn _chosenCheckBoxColumn; // Add the event-handler. _dataGridView.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler (DataGridView_EditingControlShowingTest); // No columns have been found in the event-handler yet. editingControlShowingTest_FoundColumns = 0; // _nameComboBoxColumn _nameComboBoxColumn = new DataGridViewComboBoxColumn (); _nameComboBoxColumn.HeaderText = "Name"; _dataGridView.Columns.Add (_nameComboBoxColumn); // _firstNameTextBoxColumn _firstNameTextBoxColumn = new DataGridViewTextBoxColumn (); _firstNameTextBoxColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; _firstNameTextBoxColumn.HeaderText = "First Name"; _dataGridView.Columns.Add (_firstNameTextBoxColumn); // _chosenCheckBoxColumn _chosenCheckBoxColumn = new DataGridViewCheckBoxColumn (); _chosenCheckBoxColumn.HeaderText = "Chosen"; _dataGridView.Columns.Add (_chosenCheckBoxColumn); // .NET requires that all possible values for combo-boxes in a column // are added to the column. _nameComboBoxColumn.Items.Add ("de Icaza"); _nameComboBoxColumn.Items.Add ("Toshok"); _nameComboBoxColumn.Items.Add ("Harper"); _nameComboBoxColumn.Items.Add ("Boswell"); // Set up the contents of the data-grid. _dataGridView.Rows.Add ("de Icaza", "Miguel", true); _dataGridView.Rows.Add ("Toshok", "Chris", false); _dataGridView.Rows.Add ("Harper", "Jackson", false); _dataGridView.Rows.Add ("Boswell", "Steven", true); // Edit a combo-box cell. _dataGridView.CurrentCell = _dataGridView.Rows[3].Cells[0]; Assert.AreEqual (true, _dataGridView.Rows[3].Cells[0].Selected, "1-6"); Assert.AreEqual (true, _dataGridView.BeginEdit (false), "1-7"); _dataGridView.CancelEdit(); // Edit a text-box cell. _dataGridView.CurrentCell = _dataGridView.Rows[0].Cells[1]; Assert.AreEqual (false, _dataGridView.Rows[3].Cells[0].Selected, "1-8"); Assert.AreEqual (true, _dataGridView.Rows[0].Cells[1].Selected, "1-9"); Assert.AreEqual (true, _dataGridView.BeginEdit (false), "1-10"); _dataGridView.CancelEdit(); // Edit a check-box cell. _dataGridView.CurrentCell = _dataGridView.Rows[3].Cells[2]; Assert.AreEqual (false, _dataGridView.Rows[0].Cells[1].Selected, "1-11"); Assert.AreEqual (true, _dataGridView.Rows[3].Cells[2].Selected, "1-12"); Assert.AreEqual (true, _dataGridView.BeginEdit (false), "1-13"); _dataGridView.CancelEdit(); // Make sure the event-handler was called each time. // (DataGridViewCheckBoxCell isn't derived from Control, so the // EditingControlShowing event doesn't get called for it.) Assert.AreEqual (3, editingControlShowingTest_FoundColumns, "1-14"); _dataGridView.Dispose(); } }
private void CellValidate(DataGridView grd, DataGridViewCellValidatingEventArgs e) { if (e.RowIndex > -1 && (e.ColumnIndex == 3 || e.ColumnIndex == 4 || e.ColumnIndex == 5)) { if (e.FormattedValue.ToString() == "") { return; } double outDb = 0; if (double.TryParse(e.FormattedValue.ToString(), out outDb)) { e.Cancel = false; } else { e.Cancel = true; grd.CancelEdit(); } if (outDb < 0) { e.Cancel = true; grd.CancelEdit(); } } }
private void CellValidate(DataGridView grd, DataGridViewCellValidatingEventArgs e) { if (e.RowIndex > -1 && e.ColumnIndex == 4) { if (e.FormattedValue.ToString() == "") { return; } int outDb = 0; if (Int32.TryParse(e.FormattedValue.ToString(), out outDb)) { e.Cancel = false; } else { e.Cancel = true; grd.CancelEdit(); } if (outDb <= 0 || outDb > Int32.Parse(grd.Rows[e.RowIndex].Cells[3].Value.ToString())) { e.Cancel = true; grd.CancelEdit(); } } }
private DataGridView getDGV(InventoryPouch pouch) { // Add DataGrid DataGridView dgv = new DataGridView { Dock = DockStyle.Fill, Text = pouch.Type.ToString(), Name = DGVPrefix + pouch.Type, AllowUserToAddRows = false, AllowUserToDeleteRows = false, AllowUserToResizeRows = false, AllowUserToResizeColumns = false, RowHeadersVisible = false, //ColumnHeadersVisible = false, MultiSelect = false, ShowEditingIcon = false, EditMode = DataGridViewEditMode.EditOnEnter, ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single, ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize, SelectionMode = DataGridViewSelectionMode.CellSelect, CellBorderStyle = DataGridViewCellBorderStyle.None, }; int c = 0; DataGridViewComboBoxColumn dgvItemVal = new DataGridViewComboBoxColumn { HeaderText = "Item", DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing, DisplayIndex = c++, Width = 135, FlatStyle = FlatStyle.Flat }; DataGridViewColumn dgvIndex = new DataGridViewTextBoxColumn(); { dgvIndex.HeaderText = "Count"; dgvIndex.DisplayIndex = c++; dgvIndex.Width = 45; dgvIndex.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; } dgv.Columns.Add(dgvItemVal); dgv.Columns.Add(dgvIndex); if (HasFreeSpace) { DataGridViewCheckBoxColumn dgvFree = new DataGridViewCheckBoxColumn { HeaderText = "Free", DisplayIndex = c++, Width = 40, FlatStyle = FlatStyle.Flat }; dgv.Columns.Add(dgvFree); } if (HasFreeSpace) { DataGridViewCheckBoxColumn dgvNew = new DataGridViewCheckBoxColumn { HeaderText = "NEW", DisplayIndex = c++, Width = 40, FlatStyle = FlatStyle.Flat }; dgv.Columns.Add(dgvNew); } var itemcount = pouch.Items.Length; string[] itemarr = Main.HaX ? (string[])itemlist.Clone() : getItems(pouch.LegalItems); var combo = dgv.Columns[0] as DataGridViewComboBoxColumn; foreach (string t in itemarr) combo.Items.Add(t); // add only the Item Names dgv.Rows.Add(itemcount > 0 ? itemcount : itemarr.Length); dgv.CancelEdit(); return dgv; }