Solved

Datagrid to csv

Posted on 2011-03-08
5
633 Views
Last Modified: 2012-05-11
Hi I'm trying to get a datagrid to append to a csv file but im getting errors, can anyone see what im doing wrong?

The error is;

Error      4      'System.Web.UI.WebControls.DataGrid' does not contain a definition for 'Rows' and no extension method 'Rows' accepting a first argument of type 'System.Web.UI.WebControls.DataGrid' could be found (are you missing a using directive or an assembly reference?)      T:\Web Templates\Templates\DecDIY.aspx.cs      58      67      Templates

   
Thanks!!
StreamWriter sw = File.AppendText("e:\\results\\decdiy.csv");

            for (int i = 0; i < layoutsGrid.Rows.Count; i++)
            {
                string strRowVal = "";
                for (int j = 0; j < layoutsGrid.Rows[i].Cells.Count; j++)
                {
                    if (strRowVal == "")
                    {
                        strRowVal = layoutsGrid.Rows[i].Cells[j].Text;
                    }
                    else
                    {
                        strRowVal = strRowVal + "," + layoutsGrid.Rows[i].Cells[j].Text;
                    }
                }
                sw.WriteLine(strRowVal);
            }
            sw.Close(); 

}

Open in new window

0
Comment
Question by:Gaz124
5 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 35071411
Does it have Items instead ?
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 35071535
the error says:

"are you missing a using directive or an assembly reference"

Thats rather telling, don't you think?
0
 
LVL 7

Expert Comment

by:dimaj
ID: 35072379
You're using the DataGrid from the WebControls library...
There is no Rows property.

Here's what you should do:

foreach (DataGridItem item in layoutsGrid.Items) {
  string str = "";
  for (int i = 0; i < item.Cells.Count; i++) {
    str += item.Cells[j].Text + ",";
  }
   str = str.Substring(0, str.length - 1);
}

Some of the syntax might be incorrect, but you get the picture.

Here's an article from MSDN: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datagrid.items.aspx#Y500
0
 

Author Comment

by:Gaz124
ID: 35073750
ok I tried that but all i get is a blank csv?
//seperate datagrid to comma seperate values
            string strRowVal = "";
            foreach (DataGridItem item in layoutsGrid.Items)
            {
                string str = "";
                for (int i = 0; i < item.Cells.Count; i++)
                {
                    str += item.Cells[i].Text + ",";
                }
                str = str.Substring(0, str.Length - 1);
            }
           

            //append to csv file
            StreamWriter sw = File.AppendText("e:\\results\\decdiy.csv");
            {
                
                sw.WriteLine(strRowVal);
            }
            sw.Close();

Open in new window

0
 
LVL 7

Accepted Solution

by:
dimaj earned 500 total points
ID: 35074235
you need to add this code after str = str.substring...
strRowVal += str + Environment.NewLine;
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now