I have a 15x15 dataGridView which on creation has empty (has no values) cells. Then, after user input the data, I want to get the column header which has non empty data in its row. I've been trying the following code:
DataTable dt = new DataTable();
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
bool empty = true;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[col.Index].Value!=null)
{
empty = false;
break;
}
}
if (empty == false)
{
dt.Columns.Add(col.HeaderText);
}
}
However, it doesn't appear to be working.
DataTable dt = new DataTable();
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
bool empty = false;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[col.Index].Value.ToString() == string.Empty)
{
empty = true;
}
break;
}
if (empty == false)
{
dt.Columns.Add(col.HeaderText);
}
}
Related
I've uploaded data to a datatable from a excel file. Whats the best way to check empty rows in datatable. NB: Need solution for Visual Studio 2005. Cant use linq.
foreach (DataRow row in result.Rows)
{
//check if row is empty
//if not continue processing data
//else remove the row from datatable
}
Currently the solution in my mind is inside foreach loop, put another foreach loop and check each column. If all columns are null, remove the row from datatable.
Is there any other best method. Does the above method takes more time to execute as there is another for each loop. There will be lots of rows in the datatable.
Edited:
I've got one code:
private bool checkIfRowEmpty(DataRow row)
{
var r = (DataRow)row;
int emptyCount = 0;
int itemArrayCount = r.ItemArray.Length;
foreach (var i in r.ItemArray) if (string.IsNullOrWhiteSpace(i.ToString())) emptyCount++;
if (emptyCount == itemArrayCount) return false;
else return true;
}
Is this okay? Or should I use any solution from below? Which one is good?
Here is one approach - to iterate through all rows and all columns
DataTable table = new DataTable();
List<int> rowsToRemove = new List<int>();
for (int i = 0; i < table.Rows.Count; i++)
{
DataRow row = table.Rows[i];
foreach (DataColumn col in row.Table.Columns)
{
bool skip = false;
if (row[col] != null)
{
//this column is not null, so mark skip flag as true
skip = true;
}
if (skip)
//this row should be skipped because it has at least one column that isn't null
break;
else
rowsToRemove.Add(i);
//mark this row's index for deletion
}
}
//loop through list in reverse order and remove rows by their index
for (int i = rowsToRemove.Count; i > 0; i--)
table.Rows.RemoveAt(i);
Best solution I can think of is the following one:
public bool IsRowEmpty(DataRow row)
{
if (row == null)
return true;
foreach(var value in row.ItemArray)
{
if (value != null)
return false;
}
return true;
}
Then from your main code:
foreach (DataRow row in result.Rows)
{
if(!IsRowEmpty(row))
{
// Row is not empty.
}
}
Hello,
You can simply achive this using DataView : like this -
DataView dv = yourTable.DefaultView;
dv.Sort = "column_1,column_2";
DataTable dtNew = dv.ToTable(true, "column_1", "column_2",...,"column_n"); // please mention all columns here
dtNew.Rows.RemoveAt(0);
datagridview dgv is bind to datatable dt.
dgv_copy is new datagridview.dgv_copy is add row to dgv by using row.clone.
my code is
if (dgv_copy.Columns.Count == 0)
{
foreach (DataGridViewColumn dgvc in dgv_org.Columns)
{
dgv_copy.Columns.Add(dgvc.Clone() as DataGridViewColumn);
}
}
DataGridViewRow row = new DataGridViewRow();
for (int i = 0; i < dgv_org.Rows.Count; i++)
{
row = (DataGridViewRow)dgv_org.Rows[i].Clone();
int intColIndex = 0;
foreach (DataGridViewCell cell in dgv_org.Rows[i].Cells)
{
row.Cells[intColIndex].Value = cell.Value;
intColIndex++;
}
dgv_copy.Rows.Add(row);
}
dgv_copy.AllowUserToAddRows = false;
dgv_copy.Refresh();
source is https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewrow.clone.aspx
Because original DataGridView is bounded to the DataTable you can create copy of DataTable and boud it to the second DataGridView.
var original = (DataTable)originalDataGridView.DataSource;
var copy = original.Copy();
copyDataGridView.DataSource = copy;
Pretty sure you could just do dgv_copy.rows.add(dgv_org.Rows[i].ItemArray)
I have DataTable which I add Columns and Rows from database. Problem is I have one cell which has multiples record like province can be multiples in database. So I need to create multiple records in one cell of province. My code is
foreach (var item in attributes)
{
table.Columns.Add(item.name);
//column add
}
foreach (var item in forms)
{
string FormId = Convert.ToString(item);
var row = table.NewRow();
row["Form Id"] = FormId;
foreach (var itemdata in attributes)
{
var value = attributesData.FirstOrDefault(t => t.attributeName == itemdata.name && t.strFormId == FormId);
if (value != null && String.IsNullOrWhiteSpace(value.strFormId) == false )
{
row[itemdata.name] = value.strValue;
//row add
}
}
table.Rows.Add(row);
}
gvView.DataSource = table;
I bind this with my GridView. How to break one cell with mutliples records please help.
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...
I have two Janus GridEXs. When an specific button is clicked, I want to add rows of first grid to another grid; but I want to check that already these rows do'nt exist in the second grid, so I used this code block with linq:
DataTable dtSelectedRows = new DataTable();
dtSelectedRows = firstGrid.GetDataSource().Clone();
foreach (GridEXRow row in rows)
{
DataRow dr = ((DataRowView)row.DataRow).Row;
if (dtSelectedRows.AsEnumerable().Count() > 0)
{
if (dtSelectedRows.AsEnumerable().Where(t => t.Field<Int32>("myColumn") == Convert.ToInt32(dr["myColumn"])).Count() == 0)
{
dtSelectedRows.ImportRow(dr);
}
}
else
dtSelectedRows.ImportRow(dr);
}
}
secondGrid.SetDataSource(dtSelectedRows);
but unfortunately it did'nt work and dtSelectedRows is always empty. So I forced to rewrite the block as:
GridEXRow[] rows = firstGrid.GetCheckedRows();
DataTable dtSelectedRows = new DataTable();
dtSelectedRows = firstGrid.GetDataSource().Clone();
foreach (GridEXRow row in rows)
{
if (row.RowType == Janus.Windows.GridEX.RowType.Record)
{
DataRow dr = ((DataRowView)row.DataRow).Row;
bool rowExists = false;
foreach (DataRow r in dtSelectedRows.Rows)
{
if (Convert.ToInt32(r["myColumn"]) == Convert.ToInt32(dr["myColumn"]))
{
rowExists = true;
break;
}
}
if (!rowExists)
dtSelectedRows.ImportRow(dr);}
}
secondGrid.SetDataSource(dtSelectedRows);
and fortunately it just worked. So how can I correct the first code block?
try this :
dtSelectedRows = firstGrid.GetDataSource().AsEnumerable().ToList();
instead of :
dtSelectedRows = firstGrid.GetDataSource().Clone();