How to convert DataGridViewRowCollection to DataRow[] - c#

I want to convert DataGridViewRowCollection to DataRow[]
Is there any way like:
DataRow[] rowarray=(DataRow[])(datagridview.Rows);
Or do I have to convert each row using foreach?

You need to convert it to a DataTable first.
DataTable data = (DataTable)(datagridview.DataSource);
Then copy the rows into a DataRow array.
DataRow[] drArray = new DataRow[data.Rows.Count];
data.Rows.CopyTo(drArray, 0);

How about with Linq?
DataRow[] rows = dataGridView1.Rows.Cast<DataGridViewRow>()
.Select(r => r.DataBoundItem as DataRowView)
.Where(drv => drv != null)
.Select(drv => drv.Row)
.ToArray();

I hope this will help. Try it and let us know.
void covertDGVToRowColl()
{
DataRow[] rowColl = new DataRow[dgTemp.Rows.Count]; //dgTemp is the datagridview
DataTable dtTable = new DataTable();
foreach (DataGridViewColumn dgvColumn in dgTemp.Columns)
{
dtTable.Columns.Add(dgvColumn.Name);
}
DataRow newRow;
int i = 0;
foreach (DataGridViewRow dRow in dgTemp.Rows)
{
newRow = dtTable.NewRow();
foreach (DataGridViewColumn dgvColumn in dgTemp.Columns)
{
newRow[dgvColumn.Name] = dRow.Cells[dgvColumn.Name].Value;
}
rowColl[i] = newRow;
++i;
}
}

Related

C# - How can i get header row from datatable and arrange it vertically down a column?

How can i turn this datagridview from datatable
into this
Here is my code now. Any suggestion, comments, or sample code are highly appreciated. Thank you.
DataSet result = excelReader.AsDataSet();
excelReader.Close();
if (result != null)
{
DataTable dataTable = result.Tables[0];
List<string> headers = new List<string>();
foreach (DataColumn col in dataTable.Columns)
{
headers.Add(col.ColumnName);
}
dataGridView1.DataSource = dataTable;
}
Try
string[] columnNames = dt.Columns.Cast<DataColumn>()
.Select(x => x.ColumnName)
.ToArray();
dataGridView1.DataSource = columnNames;
Or
dataGridView1.DataSource = dt.Columns.Cast<DataColumn>()
.Select(x => x.ColumnName)
.ToArray();
Simply set the Header to be visible like this:
DataGridView.ColumnHeaderVisible = true;
This worked for me.
System.Data.DataTable dt = GridDataSource.Tables[0];
foreach (System.Data.DataColumn col in dt.Columns)
{
cmbSelectSearchBy.Items.Add(col.ColumnName);
}
cmbSelectSearchBy.Text = "Select Column";

C# List<string>[] to Datatable

I need to convert a list array of type List<string>[] to Datatable in C#.
I have found many topics related to List<string[]> to Datatable conversion but nothing on the conversion I need.
Pseudocode:
//Retrieve data from MySQL server
db.Select(category, productID);
//populate List<string>[] array
list[0] = db.ListQuery[0];
list[1] = db.ListQuery[1];
//convert list[] to Datatable
.....
Any help is much appreciated.
If I'm understanding your question right, do you mean something like this?
string category = "Category";
string productId = "ProductId";
List<string[]> tempList = db.Select(category, productID); //Not necessarily correct (I'm not familiar with MySQL). Do what you need to do to create the List<string[]>
DataTable table = new DataTable();
DataRow row;
table.Columns.Add(category);
table.Columns.Add(productId);
foreach (string[] s in tempList)
{
row = table.NewRow();
row[category] = s[0];
row[productId] = s[1];
table.Rows.Add(row);
}
DataTable dataTable = new DataTable();
List<MemberInfo> props = typeof(T).GetFields().Select(objField => (MemberInfo)objField).ToList();
props.AddRange(typeof(T).GetProperties().Select(objField => (MemberInfo)objField));
if (props.Count > 0)
{
Type t;
bool tIsField = false;
for (int iCnt = 0; iCnt < props.Count; iCnt++)
{
var prop = props[iCnt];
tIsField = prop.MemberType == MemberTypes.Field;
dataTable.Columns.Add(prop.Name, tIsField ? ((FieldInfo)prop).FieldType : ((PropertyInfo)prop).PropertyType);
}
foreach (T item in data)
{
DataRow dr = dataTable.NewRow();
foreach (var field in props)
{
tIsField = field.MemberType == MemberTypes.Field;
object value = tIsField ? ((FieldInfo)field).GetValue(item) : ((PropertyInfo)field).GetValue(item, null);
dr[field.Name] = value;
}
dataTable.Rows.Add(dr);
}
}
return dataTable;

How Can Convert DataRow to DataRowView in c#

Can or how to convert DataRow to DataRowView?
For example:
DataTable dt=ds.Tables[0];
DataRow dr= dt.NewRow();
DataRowView drv = ????
Use
DataTable dt=ds.Tables[0];
DataRow dr= dt.NewRow();
DataRowView drv= dt.DefaultView[dt.Rows.IndexOf(dr)];
The above method does not work if the DataRow status is Detached.
This code snippet could be used in such case:
DataRow dRow = dataTable.NewRow();
//...
DataRowView dRowView = dRow.Table.DefaultView.AddNew();
for (int i = 0; i < dRow.ItemArray.Length; i++)
{
dRowView.Row[i] = dRow[i];
}
DataRowView selecRow = dataTable.DefaultView.Cast<DataRowView>().FirstOrDefault(a => a.Row == desRow);
works, this is a litte shorter without "where" ;-)
Use.
It also works if the DataGrid is ordered.
DataRowView selecRow = dataTable.DefaultView.Cast<DataRowView>().Where(a => a.Row == desRow).FirstOrDefault();

How to 'foreach' a column in a DataTable using C#?

How do I loop through each column in a datarow using foreach?
DataTable dtTable = new DataTable();
MySQLProcessor.DTTable(mysqlCommand, out dtTable);
foreach (DataRow dtRow in dtTable.Rows)
{
//foreach(DataColumn dc in dtRow)
}
This should work:
DataTable dtTable;
MySQLProcessor.DTTable(mysqlCommand, out dtTable);
// On all tables' rows
foreach (DataRow dtRow in dtTable.Rows)
{
// On all tables' columns
foreach(DataColumn dc in dtTable.Columns)
{
var field1 = dtRow[dc].ToString();
}
}
I believe this is what you want:
DataTable dtTable = new DataTable();
foreach (DataRow dtRow in dtTable.Rows)
{
foreach (DataColumn dc in dtRow.ItemArray)
{
}
}
You can do it like this:
DataTable dt = new DataTable("MyTable");
foreach (DataRow row in dt.Rows)
{
foreach (DataColumn column in dt.Columns)
{
if (row[column] != null) // This will check the null values also (if you want to check).
{
// Do whatever you want.
}
}
}
You can check this out. Use foreach loop over a DataColumn provided with your DataTable.
foreach(DataColumn column in dtTable.Columns)
{
// do here whatever you want to...
}
Something like this:
DataTable dt = new DataTable();
// For each row, print the values of each column.
foreach(DataRow row in dt .Rows)
{
foreach(DataColumn column in dt .Columns)
{
Console.WriteLine(row[column]);
}
}
http://msdn.microsoft.com/en-us/library/system.data.datatable.rows.aspx
In LINQ you could do something like:
foreach (var data in from DataRow row in dataTable.Rows
from DataColumn col in dataTable.Columns
where
row[col] != null
select row[col])
{
// do something with data
}
int countRow = dt.Rows.Count;
int countCol = dt.Columns.Count;
for (int iCol = 0; iCol < countCol; iCol++)
{
DataColumn col = dt.Columns[iCol];
for (int iRow = 0; iRow < countRow; iRow++)
{
object cell = dt.Rows[iRow].ItemArray[iCol];
}
}

How to get columns from a datarow?

I have a row collection (DataRow[] rows). And I want to import all rows to another DataTable (DataTable dt).
But how?
Code
DataTable dt;
if (drs.Length>0)
{
dt = new DataTable();
foreach (DataRow row in drs)
{
dt.Columns.Add(row???????)
}
// If it possible, something like that => dt.Columns.AddRange(????????)
for(int i = 0; i < drs.Length; i++)
{
dt.ImportRow(drs[i]);
}
}
Assuming the rows all have the same structure, the easiest option is to clone the old table, omitting the data:
DataTable dt = drs[0].Table.Clone();
Alternatively, something like:
foreach(DataColumn col in drs[0].Table.Columns)
{
dt.Columns.Add(col.ColumnName, col.DataType, col.Expression);
}
If your DataRows is from a Data Table with Columns defined in it,
DataRow[] rows;
DataTable table = new DataTable();
var columns = rows[0].Table.Columns;
table.Columns.AddRange(columns.Cast<DataColumn>().ToArray());
foreach (var row in rows)
{
table.Rows.Add(row.ItemArray);
}
How about
DataTable dt = new DataTable;
foreach(DataRow dr in drs)
{
dt.ImportRow(dr);
}
Note this only works if drs is a DataRowCollection. Detached rows (not in a DataRowCollection are ignored).
Don't forget to call AcceptChanges.
Try this:
// Assuming you have a DataRow object named row:
foreach(DataColumn col in row.Table.Columns)
{
// Do whatever you need to with these columns
}

Categories