Working on a group project in C# language We\'re trying to figure out how write
ID: 3771001 • Letter: W
Question
Working on a group project in C# language
We're trying to figure out how write the datagridview to the txt file each line at a time. Currently it creates a line for each cell.
The syntax to fix this issue would be greatly appreciated.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication1
{
struct Region
{
public string Region_Name { get; set; }
public string Description { get; set; }
public string Rate { get; set; }
}
public partial class RegionForm : Form
{
List<Region> regions;
DataTable dtr = new DataTable();
int y;
const string REGION_FILE = "test2.txt";
const char DELIM = ',';
public RegionForm()
{
InitializeComponent();
// initial list of regions
//regions = new List<Region>();
//regions.Add(new Region { Region_Name = "Midwest", Description = "Chicago", Rate = "2" });
//regions.Add(new Region { Region_Name = "Southeast", Description = "Miami", Rate = "1" });
//regions.Add(new Region { Region_Name = "Southwest", Description = "Austin", Rate = "3" });
//regions.Add(new Region { Region_Name = "East", Description = "New York City", Rate = "2" });
//regions.Add(new Region { Region_Name = "West", Description = "San Francisco", Rate = "5" });
//bindingSource1.DataSource = regions;
//comboBox1.DataSource = bindingSource1.DataSource;
//comboBox1.DisplayMember = "Region_Name";
//comboBox1.ValueMember = "Region_Name";
// populate the grid on the regions page
//foreach (Region c in regions)
//{
// regionGrid.Rows.Add(c.Region_Name, c.Description, c.Rate);
//}
}
private void RegionForm_Load(object sender, EventArgs e)
{
FileStream InFile = new FileStream(REGION_FILE, FileMode.Open, FileAccess.Read);
StreamReader file = new StreamReader(REGION_FILE);
dtr.Columns.Add("Region_Name", typeof(string));
dtr.Columns.Add("Description", typeof(string));
dtr.Columns.Add("Rate", typeof(double));
string newline;
while ((newline = file.ReadLine()) != null)
{
DataRow dr = dtr.NewRow();
string[] values = newline.Split(DELIM);
for (int i = 0; i < values.Length; i++)
{
dr[i] = values[i];
}
dtr.Rows.Add(dr);
}
file.Close();
InFile.Close();
regionGrid.DataSource = dtr;
}
//public void AddRegion()
//{
//}
private void label1_Click(object sender, EventArgs e)
{
}
private void regionBack_Click(object sender, EventArgs e)
{
HomeScreenForm form1 = new HomeScreenForm();
this.Hide();
form1.Show();
}
private void regionReset_Click(object sender, EventArgs e)
{
addRName.Text = null;
addRDescript.Text = null;
addRFRate.Text = null;
editFRate.Text = null;
editRName.Text = null;
editRDescript.Text = null;
}
private void addRegion_Click(object sender, EventArgs e)
{
try
{
dtr.Rows.Add(addRName.Text, addRDescript.Text, Convert.ToDouble(addRFRate.Text));
regionGrid.DataSource = dtr;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void SaveButton_Click(object sender, EventArgs e)
{
//FileStream file = new FileStream(REGION_FILE, FileMode.Create);
//StreamWriter objWriter = new StreamWriter(REGION_FILE);
//int count = regionGrid.Rows.Count;
//foreach (DataGridViewRow row in regionGrid.Rows)
//{
// objWriter.WriteLine(row.Cells[0].Value.ToString());
//}
//objWriter.Close();
//var objWriter = new StreamWriter(REGION_FILE);
//int count = carriersGrid.Rows.Count;
//foreach (Carrier c in carriers)
//{
// objWriter.WriteLine(c.SCAC + " " + c.Name + " " + c.FlatRate);
//}
StreamWriter objWriter = new StreamWriter(REGION_FILE);
for (int r = 0; r < regionGrid.Rows.Count - 1; ++r)
{
for (int c = 0; c < regionGrid.Columns.Count; ++c)
{
objWriter.WriteLine(regionGrid.Rows[r].Cells[c].Value.ToString() + DELIM);
}
}
objWriter.Close();
}
private void regionGrid_CellClick(object sender, DataGridViewCellEventArgs e)
{
y = e.RowIndex;
DataGridViewRow row = regionGrid.Rows[y];
editRName.Text = row.Cells[0].Value.ToString();
editRDescript.Text = row.Cells[1].Value.ToString();
editFRate.Text = row.Cells[2].Value.ToString();
}
private void updateRegion_Click(object sender, EventArgs e)
{
DataGridViewRow row = regionGrid.Rows[y];
row.Cells[0].Value = editRName.Text;
row.Cells[1].Value = editRDescript.Text;
row.Cells[2].Value = editFRate.Text;
}
}
}
Explanation / Answer
As you marked in bold, Change is required in SaveButton_Click() function only
I'm only writing updated SaveButton_Click() function here. Remaining all code you can use as is.
Added comments to my updates.
private void SaveButton_Click(object sender, EventArgs e)
{
StreamWriter objWriter = new StreamWriter(REGION_FILE);
for (int r = 0; r < regionGrid.Rows.Count - 1; ++r)
{
for (int c = 0; c < regionGrid.Columns.Count; ++c)
{
// Use Write function instead of WriteLine to stay in the same line
objWriter.Write(regionGrid.Rows[r].Cells[c].Value.ToString() + DELIM);
}
// Once all the columns in a row are complted, write a newline to file
// WriteLine() function without any arguments writes a line terminator to the text string
objWriter.WriteLine();
}
objWriter.Close();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.