Solved

Datagrid to csv

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

746 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

9 Experts available now in Live!

Get 1:1 Help Now