DataGridView CellContent Checkbox Change Event - c#

I have a datagridview with 3 columns: invoice id, price and a checkbox.
If checkbox is clicked price becomes 0 for that row. Now that is happening.
But when I uncheck the checkbox price should be as it was. But it is remaining zero. Below is my code for cellcontent click. How can i get previous price if checkbox in unchecked?
private void grvItems_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
var senderGrid = (DataGridView)sender;
DataGridViewRow row = this.grvItems.CurrentRow;
if (e.RowIndex >= 0)
{
if (senderGrid.Columns[e.ColumnIndex] is DataGridViewCheckBoxColumn &&
e.RowIndex >= 0)
{
if (e.ColumnIndex == grvItems.Columns["UnderWarranty"].Index)
{
string returnAmt = lblReturnAmountVal.Text;
bool isCheked = (bool)grvItems.Rows[e.RowIndex].Cells["UnderWarranty"].EditedFormattedValue;
if (isCheked)
{
grvItems.Rows[e.RowIndex].Cells["PRICE"].Value = "0.00";
lblReturnAmountVal.Text = "0.00";
}
else
{
}
grvItems.EndEdit();
}
}
}
}

Have you tried the CheckedChanged event? On Winforms and using checkboxes that should help. Untested code below:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
value = 0;
}
else
{
// whatever other logic should happen
}
}

If you want to revert to the original data value you will need to store it somewhere.
You can always use the cell's Tag property for this:
DataGridViewCell cell = grvItems.Rows[e.RowIndex].Cells["PRICE"];
if (isCheked)
{
cell.Tag = cell.Value;
cell.Value = "0.00";
lblReturnAmountVal.Text = "0.00";
}
else
{
// use your cell's datatype here!! VVV
if (cell.Tag != null) cell.Value = cell.Tag as decimal;
}

Related

Custom cell merged

I need to display data in gridview with merged rows for some columns.
the original data from database like:
please help me to display gridview like:
for column transaksi merged by tgl
for column priority , price , creted by merged by transaksi
Using C#, how can I prepare a gridview for the format? Please help me.
I suggest you to go through documentation - Tutorial: Cell Merging
To implement custom cell merge use the GridView.CellMerge event
handler. First, check if the correct column is being processed. Then,
obtain display texts for the two cells being compared. Finally,
indicate that cells are to be merged if their display texts match. Set
the CellMergeEventArgs.Handled parameter to true to override the
grid's default processing for this column.
Example:
using DevExpress.XtraGrid.Views.Grid;
// ...
private void gridView1_CellMerge(object sender, DevExpress.XtraGrid.Views.Grid.CellMergeEventArgs e) {
GridView view = sender as GridView;
if(view == null) return;
if (e.Column == colCreatorID) {
string text1 = view.GetRowCellDisplayText(e.RowHandle1, colCreatorID);
string text2 = view.GetRowCellDisplayText(e.RowHandle2, colCreatorID);
e.Merge = (text1 == text2);
e.Handled = true;
}
}
Use your own conditions while processing a column. for example take transaksi 005, In this case check values of row 5 and 6 then compare column created by for equality depends upon your condition set the e.Merge to true.
I just picked response posted on this topic and added one line to achieve what you want.
bool IsTheSameCellValue(int column, int row)
{
// To compare only values on 1st and 2nd column (TGL, TRANSAKSI)
if (column > 1) return false;
DataGridViewCell cell1 = dataGridView[column, row];
DataGridViewCell cell2 = dataGridView[column, row - 1];
if (cell1.Value == null || cell2.Value == null)
{
return false;
}
return cell1.Value.ToString() == cell2.Value.ToString();
}
private void dataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
e.AdvancedBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.None;
if (e.RowIndex < 1 || e.ColumnIndex < 0)
return;
if (IsTheSameCellValue(e.ColumnIndex, e.RowIndex))
{
e.AdvancedBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.None;
}
else
{
e.AdvancedBorderStyle.Top = dataGridView.AdvancedCellBorderStyle.Top;
}
}
private void dataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.RowIndex == 0)
return;
if (IsTheSameCellValue(e.ColumnIndex, e.RowIndex))
{
e.Value = "";
e.FormattingApplied = true;
}
}
However it's impossible to center text vertically in merged cells with this solution because it only erases the borders and not totally redraw the component.

How to find CheckBox control in DataGridView?

How to add item of checkbox in datagridview(2) to datagridview(1)
for show data in checkbox(database) on datagridview(1)
My code
DataTable a = tablebill();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
bool checkBoxValue = Convert.ToBoolean(row.Cells[0].Value);
if (checkBoxValue == true)
{
a.Rows.Add(row.Cells["Products"].Value);
}
else { }
}
dataGridView1.DataSource = a;
I'm assuming you want to add those values when a checkbox click event is triggered. If so, you could try the following..
private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
{
//This will indicate the end of the cell edit (checkbox checked)
if (e.ColumnIndex == dataGridView1.Columns[0].Index &&
e.RowIndex != -1)
{
dataGridView1.EndEdit();
}
}
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == dataGridView1.Columns[0].Index &&
e.RowIndex != -1)
{
//Handle your checkbox state change here
DataTable a = tablebill();
bool checkBoxValue = Convert.ToBoolean(dataGridView1.Rows[e.RowIndex].Cells[0].Value);
if (checkBoxValue == true)
{
a.Rows.Add(dataGridView1.Rows[e.RowIndex].Cells["Products"].Value);
}
else { }
dataGridView1.DataSource = a;
}
}
PS. Remember to properly add your dataGridView1 event handlers.

How to get row's and cell's index of a just edited cell in DataGrid

I'm having problem with finding row's and cell's index of a just edited cell in DataGrid.
I'm using CellEditEnding event to know when the cell has been edited.
Till now I've managed to do something like this. Col1 contains property DisplayIndex and that is index of selected column but I can't find in the same way.
private void DataGridData_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
DataGridColumn col1 = e.Column;
DataGridRow row1 = e.Row;
}
I've got it working now. This is how it looks like:
private void DataGridData_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
DataGridColumn col1 = e.Column;
DataGridRow row1 = e.Row;
int row_index = ((DataGrid)sender).ItemContainerGenerator.IndexFromContainer(row1);
int col_index = col1.DisplayIndex;
}
Late answer but for anyone who finds this question this code will work by adding this event to your datagrid:
private void dgMAQ_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
DependencyObject dep = (DependencyObject)e.EditingElement;
string newText = string.Empty;
//Check if the item being edited is a textbox
if (dep is TextBox)
{
TextBox txt = dep as TextBox;
if (txt.Text != "")
{
newText = txt.Text;
}
else
{
newText = string.Empty;
}
}
//New text is the new text that has been entered into the cell
//Check that the value is what you want it to be
double isDouble = 0;
if (double.TryParse(newText, out isDouble) == true)
{
while ((dep != null) && !(dep is DataGridCell))
{
dep = VisualTreeHelper.GetParent(dep);
}
if (dep == null)
return;
if (dep is DataGridCell)
{
DataGridCell cell = dep as DataGridCell;
// navigate further up the tree
while ((dep != null) && !(dep is DataGridRow))
{
dep = VisualTreeHelper.GetParent(dep);
}
if (dep == null)
return;
DataGridRow row = dep as DataGridRow;
int rowIndex = row.GetIndex();
int columnIndex = cell.Column.DisplayIndex;
//Check the column index. Possibly different save options for different columns
if (columnIndex == 3)
{
if (newText != string.Empty)
{
//Do what you want with newtext
}
}
}
To get the row-index without a 'sender' argument you might try:
Convert.ToInt32(e.Row.Header)
combined with an abbreviated form of Patryk's answer this gives:
private void DataGridData_CellEditEnding(DataGridCellEditEndingEventArgs e)
{
int col1 = e.Column.DisplayIndex;
int row1 = Convert.ToInt32(e.Row.Header);
}

color devex xtragrid row for the data is greater than a value

when I tryin to color rows for a constraint value it makes because of the string value like this:
private void gvTerbiyedekiDispolar_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
{
GridView View = sender as GridView;
if (e.RowHandle >= 0)
{
string category = View.GetRowCellDisplayText(e.RowHandle, View.Columns["fire"]);
if (category == "0,10")
{
e.Appearance.BackColor = Color.LightGoldenrodYellow;
}
}
}
but if I try to "color the values greater than 0.1" it give me runtime error like this code
private void gvTerbiyedekiDispolar_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
{
GridView View = sender as GridView;
if (e.RowHandle >= 0)
{
double category = Convert.ToDouble(View.GetRowCellDisplayText(e.RowHandle, View.Columns["fire"]));
if (category > 0.10)
{
e.Appearance.BackColor = Color.LightGoldenrodYellow;
}
}
}
what should I do ?
I replaced NULL values to 0 with ISNULL and it is OK.

How DataGridViewCellEventArgs helps to modify current value from a DataGridViewCell?

I'm using this event for remark the row when the CheckBoxColumn had been checked, so additionally I want to replace "programatically", the current cell[6] value from the same Row using the "Value" property but it fails cause by default the "readOnly" property is = "true".
The DataGridView1.DataSource is retrieved from a LINQ2SQL Query.
void updateStyle_DataGridViewCellEventArgs(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex == -1)
return;
else
{
DataGridView dgv = sender as DataGridView;
int vCHK = e.ColumnIndex;
if (vCHK != 0)
return;
else
{
DataGridViewCheckBoxCell temp = (DataGridViewCheckBoxCell)dgv.Rows[e.RowIndex].Cells[0];
if ((bool)temp.EditedFormattedValue == true)
{
DataGridViewTextBoxCell xrow = (DataGridViewTextBoxCell)dgv.Rows[e.RowIndex].Cells[6];
xrow.ReadOnly = false;
xrow.Value = "P";
xrow.OwningRow.DefaultCellStyle.BackColor = Color.Wheat;
}
else
{
temp.OwningRow.DefaultCellStyle.BackColor = Color.White;
}
}
}
}
I'd find the actual object/row in the DataSource, flip the boolean there, and rebind the grid. Then you can set the BackColor in the CellFormatting event.

Categories

Resources