Solved

Datagrid to csv

Posted on 2011-03-08
5
638 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

733 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