|
i have the code below and want to convert the results in var rows to a DataTable
var rows = new List<Row>();
var sr = new StreamReader(dirCSV + fileNevCSV);
while (!sr.EndOfStream)
{
string s = sr.ReadLine();
if (!String.IsNullOrEmpty(s.Trim()))
{
rows.Add(new Row(s));
}
}
sr.Close();
|
|
|
|
|
It would obviously depend on what sort of object is your Row class. Maybe your best bet would be to create the DataTable first and then add each DataRow while reading the csv files, instead of creating the List<row>.
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
|
|
|
|
|
|
Passing rows to the function below got it working.
public static DataTable ToDataTable<T>(this IList<T> data)
{
PropertyDescriptorCollection props =
TypeDescriptor.GetProperties(typeof(T));
DataTable table = new DataTable();
for(int i = 0 ; i < props.Count ; i++)
{
PropertyDescriptor prop = props[i];
table.Columns.Add(prop.Name, prop.PropertyType);
}
object[] values = new object[props.Count];
foreach (T item in data)
{
for (int i = 0; i < values.Length; i++)
{
values[i] = props[i].GetValue(item);
}
table.Rows.Add(values);
}
return table;
}
|
|
|
|
|
Best you would create a DataRow and add the content, then add it to IList<datarow> collection.
it will give a new extension method CopyToDataTable() which you can easily make conversion.
IList<datarow> iRows = new List<datarow>();
DataTable iTable = iRows.CopyToDataTable();
|
|
|
|
|
I have a project reading from XML file and than show it in datagridview. The code works great in win 7 and show the data as needed but when I tried to run in it in win xp, it reads from the file ok but only show the 1st column of the data. I don`t know what the problem is but it is not in the read process (I debugged it line by line) but ofcourse I`m missing something.
What can it be?
I don`t know it has any connection but I`m using win 7 64bits with VS 2008 32bit and win XP and VS 2008.
|
|
|
|
|
Without the relevant code fragment?
No idea at all...
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
The code to fill the datagridview is:
private void FillGridViewFromDataTable()
{
dataGridView.DataSource = m_table;
dataGridView.Columns["PrinterIP"].HeaderText = "IP כתובת";
dataGridView.Columns["PrinterIP"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["PrinterName"].HeaderText = "מזהה מדפסת";
dataGridView.Columns["PrinterName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["PrinterConnect"].HeaderText = "מחובר לרשת";
dataGridView.Columns["PrinterConnect"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["PrinterCounter"].HeaderText = "מונה";
dataGridView.Columns["PrinterCounter"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.Columns["PrinterPlace"].HeaderText = "מיקום";
dataGridView.Columns["PrinterPlace"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView.RowHeadersVisible = false;}
I know that the reading from XML file is good, so something doesn`t work in the display part. As I said before on win7 it works perfectly.
Thank you
|
|
|
|
|
Looking at the header text, I suspect that culture/font may be the issue here.
|
|
|
|
|
Thanks for the quick answer. I tried to change it to english but it still doesn`t show the data correct. I may add that in the list I have 3 printers but it only shows 1 and not all the information needed. It shows only the printer IP (1st column). What else can it be?
|
|
|
|
|
Just as an experiment, change the AutoSizeMode to None for each column.
|
|
|
|
|
 Thanks for the help but still nothing. It maybe something with the entering the data into the datagrideview:
private void ReadFromXml()
{
List<Printer> PrinterList = new List<Printer>();
TextReader textReader = new StreamReader("xml\\PrinterDetailsLi.xml");
XmlSerializer deserializer = new XmlSerializer(typeof(List<Printer>));
PrinterList = (List<Printer>)deserializer.Deserialize(textReader);
textReader.Close();
Ping pingSender = new Ping();
IPAddress address = IPAddress.Loopback;
for (int i = 0; i < PrinterList.Count; i++)
{
PrinterList PList = new PrinterList();
IPAddress ip = IPAddress.Parse(PrinterList[i].PrinterIP);
PingReply reply = pingSender.Send(ip,5);
if (reply.Status == IPStatus.Success)
{
DataRow row = m_table.NewRow();
SNMP snmp = new SNMP(PrinterList[i].PrinterIP);
string Res = snmp.connectSnmp();
row["PrinterConnect"] = "כן";
row["PrinterCounter"] = Res;
row["PrinterName"] = PrinterList[i].PrinterName;
row["PrinterIP"] = PrinterList[i].PrinterIP;
m_table.Rows.Add(row);
PList.PrintConnect = 1;
PList.PrintCounter = Res;
PList.PrintIP = PrinterList[i].PrinterIP;
PList.PrintName = PrinterList[i].PrinterName;
PList.date = DateTime.Today;
PrList.Add(PList);
}
else
{
DataRow row = m_table.NewRow();
row["PrinterConnect"] = "לא";
row["PrinterCounter"] = "--";
row["PrinterName"] = PrinterList[i].PrinterName;
row["PrinterIP"] = PrinterList[i].PrinterIP;
row["PrinterPlace"] = PrinterList[i].PrinterPlace;
m_table.Rows.Add(row);
PList.PrintConnect = 0;
PList.PrintCounter = "--";
PList.PrintIP = PrinterList[i].PrinterIP;
PList.PrintName = PrinterList[i].PrinterName;
PList.date = DateTime.Today;
PrList.Add(PList);
}
}
XmlSerializer serializer = new XmlSerializer(typeof(List<Printer>));
TextWriter Pd = new StreamWriter(("xml\\PrinterDetailsLi.xml"));
serializer.Serialize(Pd, PrinterList);
Pd.Close();
}
What else can make it work on win7 but not on win XP?
|
|
|
|
|
And you're certain that the other columns are being populated properly in m_table? Try setting AutoGenerateColumns=true to see what it thinks the rows are in the DataTable .
|
|
|
|
|
Thank for the help, I have found the problem. The font color is white. All I had to do is change the color of the font.
Thank you for the help, You helped a lot!
modified 1-Aug-12 10:45am.
|
|
|
|
|
Nah, I don't think I helped that much. Kudos to you for figuring it out, and a healthy 5 from me (now don't forget to put Solved on your original post so that we know it's been solved).
|
|
|
|
|
Thank you until the next problem/question
|
|
|
|
|
Any idea how can I convert a string to array for the below example:
string name = "John,Calvin,Steve";
convert to
string [] arr = new string[] {"John","Calvin","Steve"};
|
|
|
|
|
Start here
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
Use String.Split [^] on the common delimiter character (; in this example).
|
|
|
|
|
Just be careful with Split because it will split on delimiters within quotes -- which is not usually desired.
|
|
|
|
|
string name = "John,Calvin,Steve";
string [] arr = name.Split(new char[]{','});
|
|
|
|
|
Hi,
How can i send qerystring via server.transfer?
i send this from Page1.aspx
<code>Server.Transfer("../../Forms/PersonalWork/Default.aspx?md=Edit&jb=11&stp=1");</code>
wanna to get from Page2.aspx :
<code>string QueryStrings = Request.Url.Query;</code>
|
|
|
|
|
How many questions have you asked in the forums and you still can't work out which one you should post in? Take this to the ASP.NET forum.
|
|
|
|
|
|
And I doubt you are going to get an answer here because you posted this in the wrong forum. What details did you get when you googled for an answer?
|
|
|
|