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;
Related
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++;
}
I want to remove equal entries from DataTable.
I tried DefaultView, but it only removes the equals and not all entries which including them.
DataView view = table1.DefaultView;
DataTable tbl = view.ToTable();
return tbl;
you can do this
public DataTable RemoveDuplicate(DataTable dataTable, string columname)
{
Hashtable hashTable = new Hashtable();
List<String> duplicates = new List<String>();
foreach (DataRow datarow in dataTable.Rows)
{
if (hashTable .Contains(datarow [columname]))
{
duplicateList.Add(datarow );
}
else
{
hashTable .Add(datarow [columname], string.Empty);
}
}
//Now remove the duplicates .
foreach (DataRow datarow in duplicates )
dataTable.Rows.Remove(datarow );
return dataTable;
}
The code below choose the first row and remove the other row below with specific id. The things that i want is the last line within the id.
var dtremove = RemoveDuplicateRows(dt, "id);
This the extension
public DataTable RemoveDuplicateRows(DataTable dt, string colName)
{
Hashtable hTable = new Hashtable();
ArrayList duplicateList = new ArrayList();
//Add list of all the unique item value to hashtable, which stores combination of key, value pair.
//And add duplicate item value in arraylist.
foreach (DataRow drow in dt.Rows)
{
if (hTable.Contains(drow[colName]))
{
duplicateList.Add(drow);
}
else
hTable.Add(drow[colName], string.Empty);
}
//duplicateList.Sort();
//Removing a list of duplicate items from datatable.
foreach (DataRow dRow in duplicateList)
dt.Rows.Remove(dRow);
//Datatable which contains unique records will be return as output.
return dt;
}
The example of datatable.
id date
------------
A 1/1/2018
A 1/2/2018
A 1/3/2018
B 2/1/2018
B 2/2/2018
i want the result like this.
id date
------------
A 1/3/2018
B 2/2/2018
I tried not to change too much of your code, pretty sure it can be improved, but the changes below looks for the value and if it already exists in the dictionary, it replaces it: Thus, you will end up with the very last row instead of the first one. Also, it returns a new DataTable and the original one will stay intact.
public DataTable RemoveDuplicateRows(DataTable dt, string colName)
{
var uniqueRows = new Dictionary<string, DataRow>();
foreach (DataRow thisRrow in dt.Rows)
{
if (uniqueRows.ContainsKey(colName))
{
uniqueRows[colName] = thisRrow;
}
else
{
uniqueRows.Add(colName, thisRrow);
}
}
DataTable copy = dt.Copy();
copy.Rows.Clear();
foreach (var thisRow in uniqueRows)
{
copy.Rows.Add(thisRow.Value);
}
//Datatable which contains unique records will be return as output.
return copy;
}
I have a DataTable with two columns: File & Email
C://file1.jpg aaa#gmail.com
C://file2.jpg aaa#gmail.com
C://file3.jpg bbb#gmail.com
C://file4.jpg ccc#gmail.com
C://file5.jpg bbb#gmail.com
In my code i loop through the DataRows and send an email to Email with File attached.
Problem:
I need to somehow check if there are any other DataRows with the same Email and if so, send just one email with multiple attachments.
So the above DataTable would result in 3 emails:
file1,file2 sent to aaa#gmail.com
file3,file5 sent to bbb#gmail.com
file4 sent to ccc#gmail.com
My code sample:
foreach (DataRow row in dt.Rows) {
string file = row[0].ToString();
string email = row[1].ToString();
SendEmailWithAttachments(email,file);
}
I could pass a StringCollection or an array to my SendEmailWithAttachments() function then loop throught it and attach all the files, but how do I group these DataRows in first place
Using the GroupBy Linq extension you could handle your DataRows grouping them for the Email field, then foreach create a list of strings with the file names.
Of course, you need also to change the SendMailWithAttachments to receive as second parameter a List<string> instead of a single string
var g = dt.AsEnumerable().GroupBy(d => d.Field<string>("Email"));
foreach (var x in g)
{
List<string> files = new List<string>();
foreach (var z in x)
files.Add(z.Field<string>("File"));
SendEmailWithAttachments(email,files);
}
You could use GroupBy to group by email:
DataTable dt = new DataTable();
dt.Columns.Add("Path");
dt.Columns.Add("Email");
DataRow dr = dt.NewRow();
dr.ItemArray=new object[2]{"C://file1.jpg", "aaa#gmail.com"};
dt.Rows.Add(dr);
dr = dt.NewRow();
dr.ItemArray=new object[2]{"C://file2.jpg", "aaa#gmail.com"};
dt.Rows.Add(dr);
dr = dt.NewRow();
dr.ItemArray=new object[2]{"C://file3.jpg", "bbb#gmail.com"};
dt.Rows.Add(dr);
dr = dt.NewRow();
dr.ItemArray=new object[2]{"C://file4.jpg", "ccc#gmail.com"};
dt.Rows.Add(dr);
dr = dt.NewRow();
dr.ItemArray=new object[2]{"C://file5.jpg", "bbb#gmail.com"};
dt.Rows.Add(dr);
var grouped=dt.AsEnumerable().GroupBy(x=>x.Field<string>("Email"));
foreach (var mail in grouped)
{
List<string> filesForEmail = new List<string>();
foreach (var file in mail)
{
filesForEmail.Add(file.Field<string>("Path"));
}
SendEmailWithAttachments(mail.Key, filesForEmail);
}
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;
}
}