Solved

Datagrid to csv

Posted on 2011-03-08
5
640 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

695 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