Calculated Column DataGridView - C# - c#

I am trying to get a calculated column in DataGridView, at first I thought it was simple but now I have been struggling for an hour.
The code below works fine when editing columns:
private void ReceiptDataGrid_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
DataGridViewRow RecieptRow = RecieptDataGrid.Rows[e.RowIndex];
RecieptRow.Cells[4].Value = double.Parse(RecieptRow.Cells[2].Value.ToString()) * double.Parse(RecieptRow.Cells[3].Value.ToString());
}
But I am struggling to make the column take its value automatically when new rows are added.
I get a NullReferenceException
exception when I use the same code.
I have tried a for loop and got the same error
private void RecieptDataGrid_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
for (int i = 0; i < e.RowCount; i++)
{
DataGridViewRow RecieptRow = RecieptDataGrid.Rows[e.RowIndex + i];
RecieptRow.Cells[4].Value = double.Parse(RecieptRow.Cells[2].Value.ToString()) * double.Parse(RecieptRow.Cells[3].Value.ToString());
}
}
I Realize that it sees the cells at nulls about is there a workaround to this?
Help please.
Thank You.

you could check if your RecieotRow is a new row (with empty values) by using the IsNewRow property:
for (int i = 0; i < e.RowCount; i++)
{
DataGridViewRow RecieptRow = RecieptDataGrid.Rows[e.RowIndex + i];
if (RecieptRow.IsNewRow) continue;
RecieptRow.Cells[4].Value = double.Parse(RecieptRow.Cells[2].Value.ToString()) * double.Parse(RecieptRow.Cells[3].Value.ToString());
}

I found the problem, it was reading the row default values as nulls.
I replaced the default properties by adding the default value to the string that adds data to the DataGrid.

Related

How to calculate value of all rows in one datagrid column

I have this code trying to make it work but it just wont.
private void Izračunaj_Click(object sender, EventArgs e) //it calculates value on click
{
int total;
foreach(dataGridView1 column in dataGridView1.Rows()) //
{
total = total + int32.Parse(column[2].ToString());
}
textbox text = total; // want to have calculated value displayed in a text box
}
Error I get:
Non-invocable member 'System.Windows.Forms.DataGridView.Rows' cannot be used like a method.
I just dont know which syntax i need to use there.
I think it should solve your problem
int sum = 0;
for (int i = 0; i < dataGridView1.Rows.Count; ++i)
{
sum += Convert.ToInt32(dataGridView1.Rows[i].Cells[2].Value);
}
Rows is a property and you are using () which designates a method. Just remove the brackets.
foreach(dataGridView1 column in dataGridView1.Rows) //No ()
If you want to use LINQ then you can do:
total = dataGridView1.Rows
.OfType<DataGridViewRow>()
.Sum(r => Convert.ToInt32(r.Cells[2]));
You need to access Cell property of the row for your calculation.
To show it in the TextBox
textBox1.text = total.ToString();

How to delete the current cell in DataGridView?

How can I delete a current cell in a DataGridView. I am trying the code below, but it is deleting all cells except the current one.
private void dgvPurchase_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
for (int i = 0; i < datagridview.Columns.Count; i++)
{
for (int j = 0; j < datagridview.Rows.Count; j++)
{
datagridview.Rows[j].Cells[i].Value="";
}
}
}
The following link describe how to achive what you want ,
Please check the link , I am sure that will help you ,
http://msdn.microsoft.com/en-us/library/yc4fsbf5.aspx
I don't know why you do deleting cell value in CellValidating Event ..
To delete value in CurrentCell .. you may try
DataGridView1.CurrentCell.Value = ""
I'm not %100 sure but you can use DataGridView.CurrentCell property like;
int yourcolumnIndex = datagridview.CurrentCell.ColumnIndex;
int yourrowIndex = datagridview.CurrentCell.RowIndex;
datagridview.Rows[yourrowIndex ].Cells[yourcolumnIndex].Value="";
Or even using DataGrid.CurrentCellChanged event could be better approach.
You can try:
DataGridName.Rows.RemoveAt(DataGridName.CurrentCell.RowIndex);

update datagridview colums by selecting value from datagridcombox is not working properly

I have DataGridView in that one combobox the combobox values are loaded from one table after selecting combobox value i want to update other columns with respective data but this is working only for one row i want update all row.. please give any suggestion for code change.
private void dataGridView2_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView2.IsCurrentCellDirty)
{
for (int i = 0; i < (dataGridView2.Rows.Count)-1; i++)
{
try
{
if (dataGridView2.Rows[i].Cells[1].Value.ToString() != "")
{
ConnectionDB gridRdata = new ConnectionDB("SELECT * FROM Ready_Made_Master WHERE RM_Name='" + dataGridView2.Rows[i].Cells[1].Value.ToString() + "';");
DataTable redydata = gridRdata.returntable();
dataGridView2.Rows[i].Cells[2].Value = redydata.Rows[i][2].ToString();
}
}
catch
{
}
}
}
}
After the for loop,try rebinding the gridview ie
for (int i = 0; i < (dataGridView2.Rows.Count)-1; i++)
{
}
ConnectionDB gridRdata = new ConnectionDB("SELECT * FROM Ready_Made_Master");
DataTable redydata = gridRdata.returntable();
gridRdata .Datasource=redydata ;
gridRdata .Databind();
Please make the necessary chnges in Select Statement.

How to get DataGridView cell value in messagebox?

How can I get DataGridView cell value to be written in the MessageBox in C#?
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
{
MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
}
}
You can use the DataGridViewCell.Value Property to retrieve the value stored in a particular cell.
So to retrieve the value of the 'first' selected Cell and display in a MessageBox, you can:
MessageBox.Show(dataGridView1.SelectedCells[0].Value.ToString());
The above probably isn't exactly what you need to do. If you provide more details we can provide better help.
MessageBox.Show(" Value at 0,0" + DataGridView1.Rows[0].Cells[0].Value );
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
MessageBox.Show(Convert.ToString(dataGridView1.CurrentCell.Value));
}
a bit late but hope it helps
try
{
for (int rows = 0; rows < dataGridView1.Rows.Count; rows++)
{
for (int col = 0; col < dataGridView1.Rows[rows].Cells.Count; col++)
{
s1 = dataGridView1.Rows[0].Cells[0].Value.ToString();
label20.Text = s1;
}
}
}
catch (Exception ex)
{
MessageBox.Show("try again"+ex);
}
I added this to the Button of a datagrid to get the values of the cells in the row that the user is clicking:
string DGCell = dataGridView1.Rows[e.RowIndex].Cells[X].Value.ToString();
where X is the cell you want to check. Datagrid column count starts at 1 not 0 in my case. Not sure if it is default of a datagrid or because I am using SQL to populate the info.
Sum all cells
double X=0;
if (datagrid.Rows.Count-1 > 0)
{
for(int i = 0; i < datagrid.Rows.Count-1; i++)
{
for(int j = 0; j < datagrid.Rows.Count-1; j++)
{
X+=Convert.ToDouble(datagrid.Rows[i].Cells[j].Value.ToString());
}
}
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
int rowIndex = e.RowIndex; // Get the order of the current row
DataGridViewRow row = dataGridView1.Rows[rowIndex];//Store the value of the current row in a variable
MessageBox.Show(row.Cells[rowIndex].Value.ToString());//show message for current row
}

How to remove row which has one or more empty or null cell?

I have datagridview on my winform. I am displaying records in the datagridview. Now after displaying the records on the datagridview, I want to remove the row from datagridview which has one or more empy cells that is no value in the cell for that row. So for that I am checking each cell for every row if there is any cell empty or null then I remove that rows using RemoveAt() function.
My code is :
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
if (string.IsNullOrEmpty(dataGridView1.Rows[i].Cells[j].Value.ToString()))
{
dataGridView1.Rows.RemoveAt(i);
break;
}
}
}
Then problem is it does not work properly that it does not remove all the rows which has empty cell. So what should I do here ?
The simplest way to do what you want is to loop through the datagridview rows in reverse. This way your indices stay correct.
for (int i = dataGridView1.Rows.Count -1; i >= 0; i--)
{
DataGridViewRow dataGridViewRow = dataGridView1.Rows[i];
foreach (DataGridViewCell cell in dataGridViewRow.Cells)
{
string val = cell.Value as string;
if (string.IsNullOrEmpty(val))
{
if (!dataGridViewRow.IsNewRow)
{
dataGridView1.Rows.Remove(dataGridViewRow);
break;
}
}
}
}
I'm doing a couple of extra things that you may not need to do (the code is just cut and pasted from my test application)
I usually grab the row in question into a DataGridViewRow object.
I am checking the IsNewRow property because my grid was editable.
I am assigning the value to a string variable (with an as to cast it) since the way you had it was throwing an exception for me.

Categories

Resources