表示用にデータセット/データグリッドビュー コントロールに設定した配列に .csv ファイルを正常にロードしました。私が抱えている問題は、次の方法を考えることです。
- インポートされたデータの列を選択します (UI に関して)
- レビュー用に新鮮な情報を保存する
私が持っている現在のコードにより、ユーザーはシステム上のファイルを参照し、データを配列に保存してデータテーブルに設定できます。
private void btnBrowse_Click(object sender, EventArgs e)
{
#region Local Variables
int nRows = 0; //Row Counter
int icol = 0; //Index value for Columns -> fArray
int irow = 0; //Index value for Row -> fArray
string Line; //Temp string value for storing lines
#endregion
#region File Open Parameters
//file open parameters
OpenFileDialog fopen = new OpenFileDialog();
fopen.Title = "Choose file...";
fopen.Filter = "Comma Seperated Values|*.csv";
fopen.InitialDirectory = @"C:\";
#endregion
if (fopen.ShowDialog() == DialogResult.OK) //Show file open dialog and check if ok has been pressed
{
#region progress bar initialisation
//initialize progress bar
pb.Location = new Point(60, 209);
pb.Width = 516;
pb.Height = 23;
pb.Style = ProgressBarStyle.Continuous;
pnlStep1.Controls.Add(pb);
#endregion
try
{
StreamReader fReader = new StreamReader(fopen.FileName);
#region Array Index Counters
//Count Columns for fArray index
string count = fReader.ReadLine();
string[] tmpCount = count.Split(',');
fReader.Close();
//Count Rows for fArray index
fReader = new StreamReader(fopen.FileName); //begin reading from line 1
while ((fReader.ReadLine()) != null)
{
nRows++;
}
fArray = new string[nRows, tmpCount.Length];
fReader.Close();
//End count
#endregion
#region Load File Contents
fReader = new StreamReader(fopen.FileName); //begin reading from line 1
DataSet ds = new DataSet();
dt = ds.Tables.Add("ImportData");
while ((Line = fReader.ReadLine()) != null)
{
string[] row = Line.Split(',');
foreach (string column in row)
{
fArray[irow, icol] = column;
icol++;
}
icol = 0;
recordCountLabel.Text = irow.ToString(); //NEEDS LOOKING AT
pb.Value = (irow * 100) / fArray.GetLength(0);
irow++;
}
pb.Value = 100;
fReader.Close();
#endregion
#region Add data to dgImport
for (int i = 0; i < fArray.GetLength(1); i++) //Add Columns to empty DataGridView
{
dt.Columns.Add("Field " + i.ToString());
}
for (int i = 0; i < 3; i++) //Insert data into DataGridView (use fArray.GetLength(0) for entire database or use 5 for sample data)
{
DataRow row = dt.NewRow();
for (int j = 0; j < fArray.GetLength(1); j++)
{
row[j] = fArray[i, j].Trim();
}
dt.Rows.Add(row);
dgImport.DataSource = dt;
}
btnNext2.Enabled = true;
#endregion
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk.\r\nOriginal error: " + ex.Message);
}
}
}
どんな助けでも大歓迎です:)。
編集: もう一度言い忘れて申し訳ありませんが、datagridview コントロールにデータのサンプル行をいくつか表示し、動的に作成されたチェックボックスのリストを提供して、ユーザーが選択したい列を選択できるようにすることを検討していました (基本的に、選択されたデータのみをコピーします)。