Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Output Data from a DataGrid
 

Output Data from a DataGrid

Hi experts,

I have created a form using c#. This particular form includes data housed within SQL compact editiona. Iadded this data via thedatagrid view tomy form. I would like to have the ability to output data (int or string) of a specific row within the datagrid, to a txt file. Ideally, I would like to create a box, enter my primary key, and then click a button that will cause the data to output to .txt file.

I'm new to C# and would truly appreciate your insight!!!

KINGM56  Friday, June 26, 2009 7:54 PM
Hi KINGM56,

If you want to store these data so it can be reload to the form, you'd better save it as an xml file, the WriteXml method of DataTable should be the easiest way. (DataTable is your DataGridView binded datasource)
http://msdn.microsoft.com/en-us/library/3hyx9hb4.aspx

If you want to store the table as a format so your user can read it. You'd better choose a format first, e.g. The following example show a table using "|" as separator.
private void DGVToTxt(DataGridView dataGridView, string fileName)
{
string text = "";
foreach (DataGridViewColumn dgvCol in dataGridView.Columns)
{
text += dgvCol.HeaderText;
text += " | ";
}
text += "\r\n";
foreach (DataGridViewRow dgvRow in dataGridView.Rows)
{
if (dgvRow.IsNewRow == false)
{
foreach (DataGridViewCell dgvCell in dgvRow.Cells)
{
text += dgvCell.Value.ToString();
text += " | ";
}
text += "\r\n";
}
}
if (!File.Exists(fileName))
{
FileStream fs = File.Create(fileName);
fs.Close();
}
File.WriteAllText(fileName, text);
}

Hope this helps.

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Tuesday, June 30, 2009 2:43 AM
Hi KINGM56,

If you want to store these data so it can be reload to the form, you'd better save it as an xml file, the WriteXml method of DataTable should be the easiest way. (DataTable is your DataGridView binded datasource)
http://msdn.microsoft.com/en-us/library/3hyx9hb4.aspx

If you want to store the table as a format so your user can read it. You'd better choose a format first, e.g. The following example show a table using "|" as separator.
private void DGVToTxt(DataGridView dataGridView, string fileName)
{
string text = "";
foreach (DataGridViewColumn dgvCol in dataGridView.Columns)
{
text += dgvCol.HeaderText;
text += " | ";
}
text += "\r\n";
foreach (DataGridViewRow dgvRow in dataGridView.Rows)
{
if (dgvRow.IsNewRow == false)
{
foreach (DataGridViewCell dgvCell in dgvRow.Cells)
{
text += dgvCell.Value.ToString();
text += " | ";
}
text += "\r\n";
}
}
if (!File.Exists(fileName))
{
FileStream fs = File.Create(fileName);
fs.Close();
}
File.WriteAllText(fileName, text);
}

Hope this helps.

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Tuesday, June 30, 2009 2:43 AM

Thank you, Kira....that was perfect!!!

KINGM56  Thursday, July 23, 2009 7:10 PM

You can use google to search for other answers

Custom Search

More Threads

• local windows in IDE - What control
• ProgressBar animation?
• Can sub-properties be hidden from the property grid?
• CustomControl with SplitContainer crashes VS2008 designer
• MonthCalendar: HighLight Only Selected Range.
• Custom treeview control Nodes(collection) property
• PRB - Custom DataGridViewColumn (eg From the samples - CalendarColumn, MaskedColumn)
• Problem when adding a custom control to a form
• Windows Designer Bug when using ConnectionString in user control.
• IDE Properties