Datagridview column total - c#

I tried to calculate the total of the column named QTY in the datagridview.
But I coul not.My code is below. Could you help me , please?
public int RowCount { get; set; }
[BrowsableAttribute(false)]
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
private void UpdateLabelText()
{
int QtyTotal = 0;
int counter;
// Iterate through all the rows and sum up the appropriate columns.
for (counter = 0; counter < (dataGridView1.Rows.Count);
counter++)
{
if (dataGridView1.Rows[counter].Cells["Qty"].Value
!= null)
{
if (dataGridView1.Rows[counter].
Cells["Qty"].Value.ToString().Length != 0)
{
QtyTotal += int.Parse(dataGridView1.Rows[counter].
Cells["Qty"].Value.ToString());
}
}
}
// Set the labels to reflect the current state of the DataGridView.
label17.Text = "Qty Total: " + QtyTotal.ToString();
}

Try this:
int sum = 0;
int col_index=YOUR_COLUMN_INDEX;
for (int i = 0; i < dataGridView1.Rows.Count; ++i)
{
sum += Convert.ToInt32(dataGridView1.Rows[i].Cells[col_index].Value);
}
label17.Text= sum.ToString();

Try this
foreach (DataGridViewRow item in dataGridView2.Rows)
{
int n = item.Index;
text_tot.Text = (int.Parse(label17.Text) + int.Parse(dataGridView1.Rows[n].Cells[(index)].Value.ToString())).ToString();
}

Related

Merge rows in datagridview

I am trying to make a billing software and want to merge the rows with same barcode with a press of a button, and if that row contains remark then the row should not merge.
private void button3_Click(object sender, EventArgs e)
{
int GROW = dataGridView1.RowCount;
for(int i=0; i<GROW; i++)
{
DataGridViewRow row= dataGridView1.Rows[i];
string A = row.Cells[0].Value.ToString();
for(int j = 0; j< GROW; j++)
{
if(j == i)
{
}
else
{
DataGridViewRow rowb= dataGridView1.Rows[j];
string B = rowb.Cells[0].Value.ToString();
if (A == B)
{
string rema = row.Cells[8].Value.ToString();
string remb = rowb.Cells[8].Value.ToString();
if(rema == "" && remb == "")
{
string qa = row.Cells[2].Value.ToString();
string qb = rowb.Cells[2].Value.ToString();
decimal qad = Convert.ToDecimal(qa);
decimal qbd = Convert.ToDecimal(qb);
decimal tqd = qad + qbd;
string ra = row.Cells[7].Value.ToString();
string rb = rowb.Cells[7].Value.ToString();
decimal rad = Convert.ToDecimal(ra);
decimal rbd = Convert.ToDecimal(rb);
decimal trd = rad + rbd;
row.Cells[7].Value = trd;
row.Cells[2].Value= tqd;
dataGridView1.Rows.RemoveAt(j);
// i = i - 1;
GROW--;
}
}
}
}
}
Assuming your value is in Cell[2] and remark in Cell[8], something like this should work:
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
int k = 0;
for (int j = 0; j < dataGridView1.Rows.Count; j++)
{
// Check if values are same but remarks are different
if (dataGridView1.Rows[i].Cells[2].Value == dataGridView1.Rows[j].Cells[2].Value && dataGridView1.Rows[i].Cells[8].Value != dataGridView1.Rows[j].Cells[8].Value)
{
if (k != 0)
{
items.Rows.RemoveAt(j);
dataGridView1.DataSource = items;
}
k++;
}
}
}
NOTE: I have not tested this code but I hope you get the idea

Dynamically generating textboxes asp.net

I have created a function to dynamically create textboxes based on the amount selected from the textbox, additionally I'm using these textboxes to display data from database. However when the user chooses for exactly five from the dropdownlist, and three textboxes was already there, instead of adding 2 more textboxes, it adds the additional 5 textboxes. What I do in order to just add the additionaly textboxes?
protected void TotalSeal_SelectedIndexChanged(object sender, EventArgs e)
{
populate();
}
public void populate()
{
int count = Convert.ToInt32(TotalSeal.SelectedItem.Value);
for (int i = 0; i < count; i++)
{
if (i < 0)
{
}
else
{
TextBox tx = new TextBox();
tx.MaxLength = 10;
tx.Width = 100;
phSealNum.Controls.Add(tx);
phSealNum.Controls.Add(new LiteralControl(" "));
ControlCache.Add(tx);
}
}
}
UPDATE
public void populate()
{
//ControlCache = new List<Control>();
//phSealNum.Controls.Clear();
int targetCount = Convert.ToInt32(TotalSeal.SelectedItem.Value);
int currentItems = phSealNum.Controls.OfType<TextBox>().Count();
int totalitems = targetCount - currentItems;
if (totalitems <= 7)
{
for (int i = 0; i < totalitems; i++)
{
TextBox tx = new TextBox();
tx.MaxLength = 10;
tx.Width = 100;
phSealNum.Controls.Add(tx);
phSealNum.Controls.Add(new LiteralControl(" "));
ControlCache.Add(tx);
}
}
else
{
lblError.Text = targetCount + " exceeds number of seals";
}
}
Using #indrit-kello logic should be like this:
protected void TotalSeal_SelectedIndexChanged(object sender, EventArgs e)
{
populate();
}
public void populate()
{
int targetCount = Convert.ToInt32(TotalSeal.SelectedItem.Value);
if(targetCount > 7)
targetCount = 7;
int currentItems = 0;//TODO
for (int i = currentItems; i < targetCount; i++)
{
TextBox tx = new TextBox();
tx.MaxLength = 10;
tx.Width = 100;
phSealNum.Controls.Add(tx);
phSealNum.Controls.Add(new LiteralControl(" "));
ControlCache.Add(tx);
}
}

How to change exact text value become consist or contains?

Is there a way to convert it, from exact value text become consist of? So I don't need to type Ballet instead of Bal.
Here's the code:
private void button6_Click_1(object sender, EventArgs e)
{
ColumnView View = gridControl1.MainView as ColumnView;
View.BeginUpdate();
try
{
int rowHandle = 0;
DevExpress.XtraGrid.Columns.GridColumn col = View.Columns["genre"];
while (true)
{
// // Locate the next row
rowHandle = View.LocateByValue(rowHandle, col, textBox6.Text);
// // Exit the loop if no row is found
if (rowHandle == DevExpress.XtraGrid.GridControl.InvalidRowHandle)
break;
//// Perform specific operations on the found row
gridView1.FocusedRowHandle = rowHandle;
rowHandle++;
}
}
finally { View.EndUpdate(); }
}
for (int i = 0; i < gridView1.VisibleRowCount; i++)
{
var row = gridView1.GetDataRow(i);
var genre = row["ColumnName"].ToString(); //ColumnName is your genre Column name
if(genre.StartsWith(textBox6.text)){
//here you can set row sellected
}
}
I dont have experience with devexpress but you can try it like this.
i dont if this what u seek, but it solved my own problem
for (int i = 0; i < gridView1.RowCount; i++)
{
var rosw = gridView1.GetDataRow(i);
var genre = rosw["genre"].ToString();
int tmpg = 0;
// //tmpg = genre.IndexOf(textBox8.Text, StringComparison.OrdinalIgnoreCase);
if (genre.IndexOf(textBox8.Text, StringComparison.OrdinalIgnoreCase) >= 0)
{
//if (tmpg >= 1)
// MessageBox.Show(genre);
gridView1.FocusedRowHandle = i;
break;
}
}

how to manage the count value when an element is deleted or added

Hi i am new to programming in c# wpf i just need to create a datagrid of some details i made it and the one major task is the while adding the serial number will be incremented as 1,2,3,4,etc.as when i remove an record from the datagrid the serial number to rearrange according to it eg:sno:1,2,3,4,5,6.
after removing the 3rd row it should be 1,2,3,4,5 instaed of 1,2,4,5,6.
similarly for inserting a row between 1,2,3 after adding it should be 1,2,3,4 task is row nust be added between 2 and 3 the new row help need
void mbtninsertstep_Click(object sender, RoutedEventArgs e) {
int rowindex = mdatagridedit.Items.IndexOf(mdatagridedit.CurrentCell);
if (rowindex >= 0) {
int rowcount = programtable.Rows.Count;
msteps.Add(new Steps { mStepno = count, mPosition = "0",
mRepeat = "NONE", mCount = "1", mAftercut = "NONE" });
int p = rowindex + 1;
for (int i = 0; i < rowcount + 1; i++) {
programtable.Rows[i][0] = p++;
} edited = true;
}
}
delete button code
insert button code.
void mbtndeletestep_Click(object sender, RoutedEventArgs e)
{
int deleterow;
DataGridView dg = new DataGridView();
// msteps.Remove((Steps)mdatagridedit.SelectedItem);
int rowindex = dg.CurrentRow.Index;
if (rowindex >= 0) {
int rowcount = programtable.Rows.Count;
int temp = dg.CurrentCell.RowIndex;
programtable.Rows.RemoveAt(temp);
int p = temp + 1;
for (int i = rowindex; i < rowcount - 1; i++) {
programtable.Rows[i][0] = p++;
}
int RowCountAfterDeleting = programtable.Rows.Count;
}
//int p = mdatagridedit.Items.Count;
// if (mdatagridedit.SelectedItem == null) {
// System.Windows.Forms.MessageBox.Show("Select an row");
// } else {
//msteps.RemoveAt(mdatagridedit.SelectedIndex);
//int p = mdatagridedit.SelectedIndex + 1;
// for(int i = mdatagridedit.SelectedIndex;
i < mdatagridedit.Items.Count - 1; i++){
// Steps step = new Steps();
// step.mStepno = p - 1;
// } int p1 = programtable.Rows.Count;
//}
}

summing selected values of datagridview

I using this code for sum selected cells. Its work good but when user selecte cell where is letter is throws exceptions : ) how can i secure when in selectet cells is letters dont make sum
i asked same question but there i cannot add my code
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
for (int i = 0; i < dataGridView1.SelectedCells.Count; i++)
{
if (!dataGridView1.SelectedCells.Contains(dataGridView1.Rows[i].Cells["cLoadName"]))
{
float nextNumber = 0;
float sumNumbers = 0;
nextNumber = float.Parse(dataGridView1.SelectedCells[i].FormattedValue.ToString());
sumNumbers += nextNumber;
tsslSumSelected.Text = "ჯამი: " + sumNumbers;
tsslTotalSelected.Text = "მონიშნული: " + dataGridView1.SelectedCells.Count.ToString();
}
else
{
}
}
}
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
for (int i = 0; i < dataGridView1.SelectedCells.Count; i++)
{
if (!dataGridView1.SelectedCells.Contains(dataGridView1.Rows[i].Cells["cLoadName"]))
{
float nextNumber = 0;
float sumNumbers = 0;
if (float.TryParse(dataGridView1.SelectedCells[i].FormattedValue.ToString(), out nextNumber))
sumNumbers += nextNumber;
tsslSumSelected.Text = "ჯამი: " + sumNumbers;
tsslTotalSelected.Text = "მონიშნული: " + dataGridView1.SelectedCells.Count.ToString();
}
else
{
}
}
}
public void ComputeGridTot()
{
try
{
decimal qty= 0;
decimal price = 0;
decimal tax = 0;
decimal Total = 0;
for (int i = 0; i < drGrdView1.Rows.Count; i++)
{
if (drGrdView1.Rows[i].Cells[7].Value.ToString() == blcnt.ToString())
{
qty += Convert.ToDecimal(drGrdView1.Rows[i].Cells[3].Value);
price += Convert.ToDecimal(drGrdView1.Rows[i].Cells[4].Value);
tax += Convert.ToDecimal(drGrdView1.Rows[i].Cells[5].Value);
Total += Convert.ToDecimal(drGrdView1.Rows[i].Cells[6].Value);
}
}
lblGTax.Text = tax.ToString();
lblGQty.Text = qty.ToString();
lblGPrice.Text = price.ToString();
lblGTot.Text = Total.ToString();
}
catch
{
}
}

Categories