I am using Winforms with C# and I am getting this result.
I have a gridview where some data are loaded and in cell click event, the data is displayed on text boxes and combobox, however, one of the combobox has gone crazy and not displaying the data on select. When debugging the code, the SelectedValue goes to null while on other combo boxes, the value changes.
Below are the codes used.
Gridview cell click event
if (e.RowIndex != -1)
{
DataGridViewRow grid = stockGridView.Rows[e.RowIndex];
txtStockId.Text = grid.Cells[0].Value.ToString();
txtBarcode.Text = grid.Cells[1].Value.ToString();
cmbQtyDescId.SelectedValue = grid.Cells[2].Value;
txtQtyDesc.Text = grid.Cells[3].Value.ToString();
txtBrandName.Text = grid.Cells[4].Value.ToString();
txtProductName.Text = grid.Cells[5].Value.ToString();
txtUnitPrice.Text = grid.Cells[6].Value.ToString();
txtRetailPrice.Text = grid.Cells[7].Value.ToString();
txtStockInHand.Text = grid.Cells[8].Value.ToString();
cmbCategory.SelectedValue = grid.Cells[9].Value;
cmbSupplier.SelectedValue = grid.Cells[10].Value;
chkIsTaxable.Checked = Convert.ToBoolean(grid.Cells[11].Value);
}
Populate Combobox. For the other combo boxes, I am using the same code
public static void FillCombo(List<QuantityDescriptionModel> quantityDescriptions, ComboBox control)
{
control.DataSource = quantityDescriptions;
control.ValueMember = "Id";
control.DisplayMember = "QtyDescription";
}
Screenshots attached
Data not loaded on cell_click
Data loaded without cell_click
Related
As stated in the title, I have noticed some weird behaviour with my Data Grid View control. The cell click event is supposed to load details from the rows into textboxes and datepicker controls. This works for some rows of the data grid view but other rows are outright ignored.
The code:
`
private void dataGridViewAdmin_CellClick(object sender, DataGridViewCellEventArgs e)//Textbox and datepicker field input automation
{
if (e.RowIndex == -1)
{
return;
}
//Automatically fills the text fields with the User's selection.
DataGridViewRow selectedRow = dataGridViewAdmin.Rows[e.RowIndex];
txtName_Field.Text = selectedRow.Cells[1].Value.ToString();
txtPhone_Field.Text = selectedRow.Cells[5].Value.ToString();
try
{
dataGridViewAdmin.Rows[e.RowIndex].Selected = true;
datepickerEnd.MaxDate = (Convert.ToDateTime(selectedRow.Cells[4].Value)).AddDays(+7);
datepickerEnd.MinDate = (Convert.ToDateTime(selectedRow.Cells[3].Value)).AddDays(+1);
datepickerStart.Value = Convert.ToDateTime(selectedRow.Cells[3].Value);
datepickerEnd.Value = Convert.ToDateTime(selectedRow.Cells[4].Value);
Booking_ID = Convert.ToInt32(selectedRow.Cells[7].Value);
lblID.Text = "Selected ID: " + Booking_ID;
lblID.Visible = true;
}
catch (ArgumentOutOfRangeException) //Catches exception thrown if user from rows of data that have values that are more than seven days apart.
{
if(datepickerEnd.MaxDate < datepickerEnd.MinDate)
{
datepickerEnd.MaxDate = datepickerEnd.MinDate.AddDays(+7);
}
}
`
I've set a breakpoint within the code to see if it breaks on cell click. It breaks for some cells and does nothing on others. Naturally, I've tried spamming the mouse in on the problem cells to no effect.
I've tried to sort the rows differently, still no change.
Your help with this question will be appreciated.
I have a working autocomplete mode in my datagridview. The only problem is that when I select the value to fill the cell, the row switches to the one below.
I need the program to remain in the same row once the user selects the value given from the autocomplete. I show my code hopefully it can help.
private void entityLinesGrid_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
//this method helps to autocomplete the data in the field selected
int column = entityLinesGrid.CurrentCell.ColumnIndex;
string titleText = entityLinesGrid.Columns[column].HeaderText;//this line is getting the column name
TextBox autoText = e.Control as TextBox;
if (autoText != null)
{
autoText.AutoCompleteMode = AutoCompleteMode.Suggest;
//according to the column name the method will gather autocomplete data
autoText.AutoCompleteCustomSource = dbConnect.dataFeeder(titleText);
autoText.AutoCompleteSource = AutoCompleteSource.CustomSource;
}
}
I have created an event handler to display data based on user selected value.
private void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
{
DataRow dataRow = gridView1.GetDataRow(e.RowHandle);
//Did some query to get firstDataRow
dataRow["UOM"] = firstDataRow["SalesUOM"].ToString();
dataRow["Description"] = firstDataRow["Description"].ToString();
dataRow["Desc2"] = firstDataRow["Desc2"].ToString();
dataRow["FurtherDescription"] = firstDataRow["FurtherDescription"].ToString();
dataRow["Qty"] = 1;
dataRow["UnitPrice"] = AutoCount.Converter.ToDecimal(firstDataRow["Price"]);
dataRow["UnitCost"] = AutoCount.Converter.ToDecimal(firstDataRow["Cost"]);
}
The problem is the value does not appear immediately, but only appear after the user click outside the row. If the value does not appear but save operation is carried out, the value will be save in database, proving the data is there but does not displayed out in the interface.
I have a combo box witch is DropDownList and i bind it to a property of a class. this Combo Box is populated with an array.
now in run time when i change selected item by mouse click every things sounds good. but when change item by arrow key any thing wont work. even textchanged event of combo box would not raise.
For ComboBoxit's really easy to use selected index changed event, instead of text changed event. It will fire by mouse or keyboard, when it changes the selection item of a ComboBox.
Example:
private void CB_Company_SelectedIndexChanged(object sender, EventArgs e)
{
if (CB_Company.SelectedItem.ToString() != "Select a company" & CB_Company.SelectedItem.ToString() != "")
{
CB_Company.BackColor = Color.White;
CB_Company.Enabled = false;
RB_Option1.Enabled = true;
RB_Option2.Enabled = true;
}
}
Populating combobox method:
private void SetDropDownItems()
{
List<DropDownModel> dropDownModel = new List<DropDownModel>();
dropDownModel.Add(new DropDownModel()
{
Name = "Select a company",
Value = ""
});
dropDownModel.Add(new DropDownModel()
{
Name = "My Company",
Value = "Comp"
});
CB_Company.DataSource = dropDownModel;
CB_Company.DisplayMember = "Name";
CB_Company.ValueMember = "Value";
}
I hope you get the idea.
I've implemented AutoComplete like this in my DataGridView on several of my textbox columns.
var source = new AutoCompleteStringCollection();
List<string> values = new List<string>();
for (int i = 0; i < dataGridRoadway.Rows.Count - 2; i++)
{
if (columnName == "Road Name")
{
values.Add(dataGridRoadway.Rows[i].Cells["Road Name"].Value.ToString());
}
}
source.AddRange(values.ToArray());
//Set the appropriate properties on the textbox control
TextBox dgvEditBox = e.Control as TextBox;
if (dgvEditBox != null)
{
dgvEditBox.AutoCompleteMode = AutoCompleteMode.Suggest;
dgvEditBox.AutoCompleteCustomSource = source;
dgvEditBox.AutoCompleteSource = AutoCompleteSource.CustomSource;
}
When an item from my AutoComplete source is selected using the mouse, the current row is incorrectly set to a new row (selecting an item from AutoComplete using the arrows and tab doesn't trigger a new row). This in turn, fires the RowValidating event and runs my validation logic on the entire row before the user is actually done entering data from that row.
I would guess that DataGridView detects my mouse click on the AutoComplete dropdown and associates it with clicking on a new row. Of course, this is not the behavior I need. Is there a way around this? Is there a way to detect an AutoComplete selection and then override the new row being created?