Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Datagrid to csv

Posted on 2011-03-08
5
Medium Priority
?
647 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 45

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 2000 total points
ID: 35074235
you need to add this code after str = str.substring...
strRowVal += str + Environment.NewLine;
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

610 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