Loop through multiple datagridview - c#

In my windows form, I am having 10 datagridview. They are exactly same, I mean columns are exactly same.
The ids are like myGrid1,myGrid2, myGrid3, myGrid4.....myGrid10.
Now I want to loop through all datagridview at once.
string prodName = "";
for (int i = 1; i <= 10; i++)
{
foreach (DataGridViewRow dr in myGrid[i].Rows)
{
prodName += dr.Cells["ProductName"].Value
}
}
but myGrid[i] doesn't exists obviously.
I can loop each datagridview seperately but is there any easy way to do this?

Hope this helps.
foreach (Control x in this.Controls)
{
if (x is DataGridView)
{
foreach (DataGridViewRow dr in (DataGridView(x)).Rows)
{
prodName += dr.Cells["ProductName"].Value
}
}
}
or
foreach ( DataGridView dgv in this.Controls.OfType<DataGridView>())
{
foreach (DataGridViewRow dr in dgv.Rows)
{
prodName += dr.Cells["ProductName"].Value
}
}

Related

filtering the data by knowing the number of bills that were not done for each employee

I want to filtering the data from dgv1 to dgv2 by knowing the number of bills that were not done for each employee separately, such as the picture:
can this code help you?
List<string> selsepersons = new List<string>();
foreach (DataGridViewRow row in dgv1.Rows)
if (!selsepersons.Contains(row.Cells["selseperson"].Value.ToString()))
selsepersons.Add(row.Cells["selseperson"].Value.ToString());
foreach (string name in selsepersons)
{
int done = 0, undone = 0;
foreach (DataGridViewRow row in dgv1.Rows)
if (row.Cells["selseperson"].Value.ToString() == name)
{
done += row.Cells["actiontype"].Value.ToString() == "done" ? 1 : 0;
undone += row.Cells["actiontype"].Value.ToString() == "not done" ? 1 : 0;
}
dgv2.Rows.Add(name, done, undone);
}

Preventing GridView from adding redundant rows

I have the following code:
foreach (DataGridViewRow row in form1.dataGridView1.SelectedRows)
{
int index = dataGridView2.Rows.Add(row.Clone() as DataGridViewRow);
foreach (DataGridViewCell cell in row.Cells)
{
dataGridView2.Rows[index].Cells[cell.ColumnIndex].Value = cell.Value;
}
So I copy rows which were selected in a GridView to another Gridview. Now I want him to check if a row is already added to the second one and open a messagebox. How to do that? I haven't found solutions which fit in my case.
foreach (DataGridViewRow row in form1.dataGridView1.SelectedRows)
{
bool isnotexist = true;
foreach (DataGridViewRow rowgrid2 in dataGridView2.Rows)
{
if (rowgrid2.Cells[0].Value.ToString() == row.Cells[0].Value.ToString())
{
isnotexist = false;
break;
}
}
if (isnotexist)
{
int index = dataGridView2.Rows.Add(row.Clone() as DataGridViewRow);
foreach (DataGridViewCell cell in row.Cells)
{
dataGridView2.Rows[index].Cells[cell.ColumnIndex].Value = cell.Value;
}
}
}
Hope this code help...

Remove Item in datagridcombocell when its already use on another row C#

Hello i want to remove a item from datagridcombocell which is use on another row in same cell like this
when i select qw2120 so on next row it will not display the same item on combo cell
how its possible?
lstCombo.Clear();
foreach (DataRow dr in dt.Rows)
{
lstCombo.Add(dr.Field<string>("ProductCode").ToString());
}
foreach (DataRow dr in dt.Rows)
{
if (e.RowIndex == 0)
ProductCode.Items.Add(dr.Field<string>("ProductCode").ToString());
else
{
ProductCode.Items.Clear();
int i = 0;
foreach (var item in lstCombo)
{
if (item.ToString() == dgDetail.Rows[e.RowIndex - 1].Cells["ProductCode"].Value.ToString())
{ lstCombo.RemoveAt(i); break; }
else i++;
}
foreach (var items in lstCombo) ;
// ProductCode.
}
}
This is my code Denise

Delete row in a DataGridView C#

I tried to loop through my dataGridView1 and remove rows which don't satisfy the condition as following:
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (!(Convert.ToDateTime(row.Cells[7].Value) - DateTime.Today).Days <= 0)
{
dataGridView1.Rows.Remove(row); //error: Uncommitted new row cannot be deleted.
}
}
But I got this error:
Uncommitted new row cannot be deleted.
I can manage if the code also VB.NET.
Don't use foreach in this case, the looped collection may be modified and leads to unpredicted result, sometimes throws exception like collection was modified (encountered mainly in LINQ), use for instead:
for(int i = dataGridView1.RowCount-1; i >= 0; i--){
var row = dataGridView1.Rows[i];
if (!row.IsNewRow&&!(Convert.ToDateTime(row.Cells[7].Value) - DateTime.Today).Days <= 0){
dataGridView1.Rows.Remove(row);
}
}
Note that we have to loop from the largest index to 0.
Try
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (!(row.Cells.OfType<DataGridViewCell>().All(c=>c.Value == null))
{
if (!(Convert.ToDateTime(row.Cells[7].Value) - DateTime.Today).Days <= 0)
{
dataGridView1.Rows.Remove(row);
}
}
}
try with putting following condition:
foreach(DataGridViewRow row in dataGridView1.Rows)
{
if(!row.IsNewRow)
{
if (!(Convert.ToDateTime(row.Cells[7].Value) - DateTime.Today).Days <= 0)
{
dataGridView1.Rows.Remove(row);
}
}
}

Code to clear DataGridView

See code below:
foreach (DataGridViewRow r in dgvStatus.Rows)
{
foreach (DataGridViewCell c in r.Cells)
{
c.Value = "";
}
}
Pretty simple, clears all the values. But I need to make some modifications to it and don't know how to go along.
I want it to clear all the values, except for the first cell in every column. Also, some cells contain checkboxes and not textboxes, how can I make a check for this?
Thanks
Try this:
foreach (DataGridViewRow r in dgvStatus.Rows) {
if (r.Index == 0) {
continue;
}
foreach (DataGridViewCell c in r.Cells) {
if c.OwningColumn is DataGridViewCheckBoxColumn) {
continue;
}
c.Value = "";
}
}

Categories