Crash on read DataGridView - c#

this code must read the values ​​from a datagridview and save them in a list, but the following error is generated: 'System.Windows.Forms.DataGridViewRow' to type 'System.Data.DataRow'.
C# Code#:
List<string> TotaleOreGiornaliere = new List<string>();
int conta = 0;
foreach (DataGridViewRow dr in dataGridViewPrincipale.Rows)
{
TotaleOreGiornaliere.Add(dr.Rows[conta].Cells[2].FormattedValue.ToString());
conta++;
}

List<string> TotaleOreGiornaliere = new List<string>();
foreach (DataGridViewRow dr in dataGridView1.Rows)
{
TotaleOreGiornaliere.Add(dr.Cells[2].FormattedValue.ToString());
}
try this above code snippet will work.
dr.Rows[conta].Cells[2].FormattedValue.ToString()
this will not work as DataGridViewRow does not contain property for Rows
kindly look at this document DataGridViewRow

Just use a DataRow:
foreach (DataRow dr in dataGridViewPrincipale.Rows) {
TotaleOreGiornaliere.Add(dr.Rows[conta].Cells[2].FormattedValue.ToString());
conta++;
}

Related

DataGridView without DataSource to XML string

I have some DataGridViews on a form, and would like to get a XML string from it.
But for some reason it gives "Exception thrown: 'System.NullReferenceException'" at the point of return.
When I look inside the foreach rows, it has the data I have put in.
What is the problem here?
public string DataGridViewToXML(DataGridView DGV)
{
DataTable DT = new DataTable();
foreach (DataGridViewColumn col in DGV.Columns) { DT.Columns.Add(col.Name); }
foreach (DataGridViewRow row in DGV.Rows)
{
DataRow dRow = DT.NewRow();
foreach (DataGridViewCell cell in row.Cells) { dRow[cell.ColumnIndex] = cell.Value; }
DT.Rows.Add(dRow);
}
return DT.DataSet.GetXml();
}
You haven't told as at what line it trows it but as i get it you copy all data from DGV to DT so you can parse DataSet.GetXml();?
If that is the case why you just do not do:
public string DataGridViewToXML(DataGridView DGV)
{
return (DGV.DataSource as DataTable).DataSet.GetXml();
}
I know it doesn't solve your null error but why would you solve it when you have easier way of doing you command. Other way tell me at what line error drops and i will try explaining why it happens.

How to find DataGridViewRow in DataGridView that is "attached" to DataRow in DataTable?

In my example i have got:
DataTable dt = SomeMethodThatFillsDataTable();
DataGridView dgv = new DataGridView;
dgv.DataSource = dt;
now I would like to "pick" some DataRows from DataTable and highlight it in DataGridView
DataRow[] foundRows = dt.Select("someColumn = someTerm");
foreach (DataRow row in foundRows)
{
DataGridViewRow dgvRow = // here i would like to get acces to DataGridViewRow "attached" to row
dgvRow.DefaultCellStyle.BackColor = Color.Red;
}
Any idee how to do this?
Is it possible anyway?
You may use this way
foreach(DataGridViewRow row in dgv.Rows)
{
if(row.Cells[someColumn].Value.ToString().Equals(someTerm))
{
dgvRow.DefaultCellStyle.BackColor = Color.Red;
}
}

How to convert DataGridViewRowCollection to DataRow[]

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

C#, Looping through dataset and show each record from a dataset column

In C#, I'm trying to loop through my dataset to show data from each row from a specific column. I want the get each date under the column name "TaskStart" and display it on a report, but its just shows the date from the first row for all rows can anybody help?
foreach (DataTable table in ds.Tables)
{
foreach (DataRow dr in table.Rows)
{
DateTime TaskStart = DateTime.Parse(
ds.Tables[0].Rows[0]["TaskStart"].ToString());
TaskStart.ToString("dd-MMMM-yyyy");
rpt.SetParameterValue("TaskStartDate", TaskStart);
}
}
I believe you intended it more this way:
foreach (DataTable table in ds.Tables)
{
foreach (DataRow dr in table.Rows)
{
DateTime TaskStart = DateTime.Parse(dr["TaskStart"].ToString());
TaskStart.ToString("dd-MMMM-yyyy");
rpt.SetParameterValue("TaskStartDate", TaskStart);
}
}
You always accessed your first row in your dataset.
DateTime TaskStart = DateTime.Parse(dr["TaskStart"].ToString());
foreach (DataRow dr in ds.Tables[0].Rows)
{
//your code here
}
foreach (DataTable table in ds.Tables)
{
foreach (DataRow dr in table.Rows)
{
var ParentId=dr["ParentId"].ToString();
}
}
foreach (DataRow table in ds.Tables[0].Rows)
{
int ForType = Convert.ToInt32(table["Fortype"].ToString());
ds.Tables[0].DefaultView.RowFilter = "ForType ='" + ForType +"'";
rbtnQuestion.DataSource = ds.Tables[0].DefaultView;
rbtnQuestion.DataBind();
}

How Convert DataSet to List<ArrayList>?

I have a dataset ds and a List of ArrayList newpath i want to add(assign) ds to newpath
how is that possible.
public List<ArrayList> newpath
{
set { ViewState["newpath"] = value; }
get
{
if (ViewState["newpath"] == null)
return new List<ArrayList>();
else
return (List<ArrayList>)ViewState["newpath"];
}
}
i am trying with
foreach (DataRow dataRow in Ftb.Rows)//Ftb is datatable
{
newpath.Add(dataRow);//newpath is List<ArrayList>
}
and
foreach (DataRow dRow in ds.Tables[0].Rows)
{
newpath.Add(dRow);
}
if i am doing like above way then i am getting the error
"The best overloaded method match for 'System.Collections.Generic.List.Add(System.Collections.ArrayList)' has some invalid arguments"
Please help me if in someway.. how to do that
I assume you want each item in newpath to contain the column values of each row?
If so, have such code instead:
foreach (DataRow dataRow in Ftb.Rows)//Ftb is datatable
{
ArrayList values = new ArrayList();
foreach (object value in dataRow.ItemArray)
values.Add(value);
newpath.Add(values);
}
change newpath from
List<ArrayList>
to
List<DataRow>
From the code you've posted it looks as if your newpath variable is a list of ArrayLists. So when you try to add dRow which is DataRow you're getting an error as it's not an ArrayList.
If you want to add DataRows I think newpath should be a list of DataRow. If you wanted to populate the ArrayList with data from the DataRow you'd need something like this -
foreach (DataRow dataRow in Ftb.Rows)//Ftb is datatable
{
ArrayList myAL = new ArrayList();
myAL.Add(dataRow["your_column_name"]);
//add fields from DataRow to array list using your required logic
newpath.Add(myAL);
}
I have modified #Shadow Wizard code and now its working fine
List<ArrayList> newval = new List<ArrayList>();
foreach (DataRow dRow in ds.Tables[0].Rows)
{
ArrayList values = new ArrayList();
foreach (object value in dRow.ItemArray)
values.Add(value);
newval.Add(values);
}
newpath = newval;

Categories