Code to clear DataGridView - c#

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 = "";
}
}

Related

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...

DataGridView Multiple Selected Columns get both row data

Thanks StackOverflow !
The Below code is intended to selected and run through each row.
foreach (DataGridViewRow row in RGV.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
if (RGV.SelectedColumns.Contains(cell.OwningColumn))
{ row.Cells["Status"].Value = "OK"; }
else
{ row.Cells["Status"].Value = "Check"; }
}
}
There is couple of errors in your code. First, you are trying to assign a DatagridViewColumn to a DatagridViewRow, which is build-able but i really dont know how it will react.
As answer to your question i would suggest something like this:
foreach (DataGridViewRow row in RGV.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
row.Cells["Status"].Value = "Check";
if (RGV.SelectedColumns.Contains(cell.OwningColumn))
{
row.Cells["Status"].Value = "OK";
break;
}
}
}
I hope i helped you somehow.

How can I copy a rows' values into new row datagridview c#

I have a datagridview with 5 columns.
For each new rowadded I want to copy the last rows' values into the new one except for one.
can anyone help me pls?
I'm a bit new to c# :/
I've been trying something like this
foreach (DataGridViewColumn Column in dataGridView1.Columns)
{
foreach (DataGridViewCell cell in dataGridView1.Rows)
{
if (cell.RowIndex != lastrowindex)
{
if (cell.FormattedValue == String.Empty)
{
listNames.Add(cell.Value.ToString());
}
}
}
}
string strNames = null;
foreach (string name in listNames)
strNames += name + Environment.NewLine;
MessageBox.Show("List of all rows\n\n" + strNames);
Is this correct?
if (cell.FormattedValue == String.Empty)
{
listNames.Add(cell.Value.ToString());
}
I can imagine that if FormattedValue is empty, than cell.Value.ToString() is also empty. If so... your collection listNames is filled with empty strings result in strNames which exists only of newlines...
Perhaps you want to change == into !=:
if (cell.FormattedValue != String.Empty)
{
listNames.Add(cell.Value.ToString());
}

How to change color of empty rows in a DataGridView?

I'm trying to change color of empty rows in a DataGridView
foreach (DataGridViewRow r in dgv1.Rows)
if (r.Value.ToString() == "")
//if (r.Cells.Value.ToString() == "") - also trying
r.DefaultCellStyle.BackColor = Color.WhiteSmoke;
but Row has no definiton for Value, and Cells need to be specified by Column.
How can I do this, please ?
Well, you'll need to check all the Cells. Here's how you can do that using LINQ:
foreach(DataGridViewRow r in dgv1.Rows) {
if(r.Cells.All(c => c.Value.ToString() == string.Empty)) {
r.DefaultCellStyle.BackColor = Color.WhiteSmoke;
}
}
foreach (DataGridViewRow r in dgv1.Rows)
if (r.Cells["YourImportantFieldNameLikeID"].Value == null)
r.DefaultCellStyle.BackColor = Color.WhiteSmoke;
if you do not want to see the last empty row you can disable it with:
DataGridViewName.AllowUserToAddRows = False;

Can't select rows in datagridview

I have a method which select rows in datagrid and change value of checkboxcolumn:
foreach (DataGridViewRow gridRow in dataGridView1.Rows)
{
if (_selectedIDs != null)
foreach (long id in _selectedIDs)
{
gridRow.Selected = false;
if ((long)((DataRowView)gridRow.DataBoundItem)["ObjectD"] == id)
gridRow.Selected = true;
}
if (_checkedIDs != null)
foreach (long id in _checkedIDs)
{
((DataRowView)gridRow.DataBoundItem)["Choosen"] = 0;
if ((long)((DataRowView)gridRow.DataBoundItem)["ObjectD"] == id)
((DataRowView)gridRow.DataBoundItem)["Choosen"]=true;
}
}
dataGridView1.Refresh();
When I'm debugging this code I see that it enters in lines
gridRow.Selected = true;
and
((DataRowView)gridRow.DataBoundItem)["Choosen"]=true;
and in quickwatch I see that properties of those rows are changed.
But after execution of this code I still have only one row selected
Does anyone have an idea what's wrong with this code?
In addition to the MultiSelect, you might want to make sure that the class you databind implements the INotifyPropertyChanged interface.
You also could set the selected = false before you start looping, or you will overwrite your selection every time...
foreach (DataGridViewRow gridRow in dataGridView1.Rows)
{
if (_selectedIDs != null)
// *****
gridRow.Selected = false;
// *****
foreach (long id in _selectedIDs)
{
if ((long)((DataRowView)gridRow.DataBoundItem)["ObjectD"] == id)
gridRow.Selected = true;
}
if (_checkedIDs != null)
foreach (long id in _checkedIDs)
{
((DataRowView)gridRow.DataBoundItem)["Choosen"] = 0;
if ((long)((DataRowView)gridRow.DataBoundItem)["ObjectD"] == id)
((DataRowView)gridRow.DataBoundItem)["Choosen"]=true;
}
}
Make sure the DataGridView.MultiSelect property is set to true.
Today, I have this error and have been fixed by set Enabled to true
dataGridView1.Enabled = true;

Categories