我有一个要求,我需要读取文件并在Datagridview中显示。
注意:一个文件包含1000行,但我必须在加载时只显示100行,单击NEXT按钮后会加载下100行。我完成了这项要求并且工作正常。
我需要在按钮点击时合并2列。单击一个按钮后,将打开一个新窗口,我们可以选择要合并的列并在Datagridview中显示。当我这样做时,它只在当前页面上合并列(即只有100行),但我希望它能为所有页面(全部1000行)完成。 谁能解决我的问题?下面是我为合并2列而粘贴的代码:
表格1:
private void btnMerge_Click(object sender, EventArgs e)
{
frmMerge fs = new frmMerge(dataGridView1);
fs.cmbColumn1.DataSource = cmbList;
for (int i = 0; i <= cmbList.Count - 1; i++)
{
fs.cmbColumn2.Items.Add(cmbList[i]);
}
fs.ShowDialog();
}
浏览按钮代码(我选择文件并将其读入datagridview):
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "Desktop";
openFileDialog1.Filter = "dat files (*.DAT)|*.DAT|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
FileName = openFileDialog1.FileName;
string text = System.IO.File.ReadAllText(FileName);
datfile = text.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
//Added on 2015-12-02
maxRec = datfile.Length - 1;
PageCount = maxRec / pageSize;
LoadPage(MyFOrmat);
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
表格2 :(点击MERGE按钮后,会打开另一个窗口,其中包含合并按钮和组合框)
private void btnMerge_Click(object sender, EventArgs e)
{
DataGridViewTextBoxColumn dgvctime = new DataGridViewTextBoxColumn();
dgvctime.Name = cmbColumn1.Text;
dgvctime.HeaderText = cmbColumn1.Text;
dataGridView1.Columns.Add(dgvctime);
dataGridView1.Rows.Add(dgvctime);
for (int i = 0; i <= dgvMerge.Rows.Count - 1; i++)
{
dataGridView1.Rows[i].Cells[cmbColumn1.Text].Value = dgvMerge.Rows[i].Cells[cmbColumn1.Text].Value + " " + dgvMerge.Rows[i].Cells[cmbColumn2.Text].Value;
}
}
LOADPAGE:
public void LoadPage(string Format, bool isFindAndReplace = false)
{
int startRec;
int endRec;
if (currentPage == PageCount)
{
endRec = maxRec;
}
else
{
endRec = pageSize * currentPage;
}
dataGridView1.Rows.Clear();
if (recNo == 0)
{
dataGridView1.Columns.Clear();
}
int rowindex = 0;
startRec = recNo;
for (int RowCount = startRec; RowCount <= endRec; RowCount++)
{
if (datfile[RowCount].ToString() != "")
{
if (RowCount == 0)
{
string[] column = datfile[RowCount].Split('þ');
for (int i = 0; i < column.Length - 1; i++)
{
if (column[i].ToString() != "" && column[i].ToString() != "\u0014")
{
DataGridViewTextBoxColumn dgvtxtcountry = new DataGridViewTextBoxColumn();
dgvtxtcountry.HeaderText = column[i].ToString();
dgvtxtcountry.Name = column[i].ToString();
dataGridView1.Columns.Add(dgvtxtcountry);
cmbList.Add(column[i]);
// dataGridView1.Columns["Sent Date"].DefaultCellStyle.Format = "dd/MM/yyyy";
i += 1;
}
}
}
if (RowCount != 0)
{
dataGridView1.Rows.Add();
string[] column = datfile[RowCount].Split('þ');
int index = 0;
for (int i = 1; i < column.Length - 1; i++)
{
if (column[i].ToString() != "\u0014")
{
if (i == 3)
{
dataGridView1.Rows[rowindex].Cells[index].Value = Convert.ToDateTime(column[i]).ToString(Format);
}
else
{ dataGridView1.Rows[rowindex].Cells[index].Value = column[i].Trim('þ'); }
if (isFindAndReplace && index == f.cmbColumnCombo.SelectedIndex)
{
if ((column[i]).Contains(f.txtfind.Text))
dataGridView1.Rows[rowindex].Cells[index].Value = column[i].Replace(f.txtfind.Text, f.txtreplace.Text);
}
index += 1;
i += 1;
}
}
rowindex += 1;
}
}
recNo += 1;
//IsDateFormatted = false;
}
}
About MyFormat:
private void DateFormat_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox objCombobox = (ComboBox)sender;
MyFOrmat = objCombobox.SelectedItem.ToString();
if (recNo > 0)
recNo = recNo - 30;
LoadPage(MyFOrmat);
}