Google
 
Web ppczone.net

View Full Version : Export Datagrid to a file


manmit.walia@gmail.com
02-21-2006, 09:24 AM
Hello all,
I am trying to build a small application that binds a datagrid to an
arraylist. I have this portion working. But the next thing I want too
do is export the datagrid to a csv or excel file. But remeber I am
building a pocket pc application. So I want to save the file in
MyDocuments/Personal in the pocket pc device. Then I will just sync the
file to my pc when I am done. I have 3 buttons on the form.

Button 1 = Add Textbox value to arraylist
Button 2 = Bind Datagrid with ArrayList as DataSource
Button 3 = Export Datagrid to a file.

Here is the code I have so far... any help would be greatful.





private void button1_Click(object sender, System.EventArgs e)
{
string i = textBox1.Text.ToString();

if(itemList.Count <= 0)
{

itemList.Add(new Items(i));
}

else
itemList.Add(new Items(i));
}

private void button2_Click(object sender, System.EventArgs e)
{
itemGrid.Visible = true;
itemGrid.DataSource = itemList;
label1.Text = itemList.Count.ToString();

}

private void button3_Click(object sender, System.EventArgs e)
{

/*

saveFileDialog1.ShowDialog();
string gloName = saveFileDialog1.FileName;
FileInfo fi = new FileInfo(saveFileDialog1.FileName);
StreamWriter sw = fi.CreateText();
for(int i = 0; i<itemList.Count -1; i++)
{
sw.Write(itemList[i].ToString());
}

for(int ii = 0; ii<itemGrid.TableStyles.Count; ii++)
{
string temp = itemGrid.TableStyles[ii].ToString();
sw.Write(temp);
}
*/



// now, I need to write the contents out

string fileName = saveFileDialog1.FileName;

ArrayList temp = new ArrayList();
foreach (DataRow theRow in ([DataTable]itemGrid.DataSource).Rows)

{

string dataItem = "";

foreach(DataColumn theColumn in
((DataTable)itemGrid.DataSource).Columns)

{

dataItem=dataItem+theRow[theColumn].ToString()+"\t";

}

dataItem = dataItem.Substring(0, dataItem.Length - 2);

temp.Add(dataItem);

}

string[] theStrings = new string[temp.Count];

temp.CopyTo(theStrings,0);
File.AppendText(fileName);
//File.WriteAllLines(fileName,theStrings);




}
}

public class Items
{

#region ### Item Class to create ArrayList ###

private string itemName;

public Items()
{
}

public Items(string IN)
{
itemName=IN;
}

public string IN
{
get
{
return itemName;
}
set
{
itemName = value;
}
}

#endregion

} // End Prod Class

Matt Hoy
02-21-2006, 10:11 AM
Yeah

Beverly Howard [Ms-MVP/MobileDev]
02-21-2006, 12:14 PM
suggest posting this at microsoft.public.pocketpc.developer or the
appropriate developer group.

Beverly Howard [MS MVP-Mobile Devices]