C# 事件功能应适用于datagridview-分页概念中的所有页面

C# 事件功能应适用于datagridview-分页概念中的所有页面,c#,datagridview,C#,Datagridview,我的任务是开发涉及分页的Windows应用程序。如果我执行诸如拆分日期和时间之类的任何事件,它将仅应用于当前页面。我想将该事件应用于Datagridview中的所有页面 如果我使用一个datatable/dataset并对其进行处理,那么UI在读取文件时会花费时间,因为它会再次将整个文件读取到数据表中。因此,请建议将事件应用于DataGridView中的所有页面的任何其他替代方案 我会张贴代码,或上传我的代码在任何网站或在这里,如果需要的话。 如果我的问题不清楚,请告诉我 变量声明: List&

我的任务是开发涉及分页的Windows应用程序。如果我执行诸如拆分日期和时间之类的任何事件,它将仅应用于当前页面。我想将该事件应用于
Datagridview
中的所有页面

如果我使用一个
datatable/dataset
并对其进行处理,那么UI在读取文件时会花费时间,因为它会再次将整个文件读取到数据表中。因此,请建议将事件应用于
DataGridView
中的所有页面的任何其他替代方案

我会张贴代码,或上传我的代码在任何网站或在这里,如果需要的话。 如果我的问题不清楚,请告诉我

变量声明:

List<String> cmbList = new List<string>();
        public String Replace;
        public String Find;
        public String Col;
        public String NewColumn;
        public String NewColumnValue;
        public string MyFOrmat { get; set; }
        int PageCount;
        int maxRec;
        int pageSize = 30;
        int currentPage = 1;
        int recNo = 0;
        string FileName;
        String[] datfile;
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);
                }
            }
        }
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]);
                                    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('þ'); }
    index += 1;
                                    i += 1;
                                }
                            }
                            rowindex += 1;
                        }
                    }
                    recNo += 1;
                }
            }
 private void btnFindandReplace_Click(object sender, EventArgs e)
        {
            Form2 f = new Form2();
            f.cmbColumnCombo.DataSource = cmbList;
            f.ShowDialog();

            for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
            {
                //dataGridView1.Rows[rowindex].Cells[index].Value = Convert.ToDateTime(column[i]).ToString(Format);
                if (dataGridView1.Rows[i].Cells[f.cmbColumnCombo.Text].Value.ToString().ToLower().Contains(f.txtfind.Text.ToLower()))
                {
                    //dataGridView1.Rows[i].Cells[f.cmbColumnCombo.Text].Value = dataGridView1.Rows[i].Cells[f.cmbColumnCombo.Text].Value.ToString().ToLower().Replace(f.txtfind.Text.ToLower(), f.txtreplace.Text);
                    //bulidDataRow(i);
                    if (!string.IsNullOrEmpty(f.txtfind.Text))
                    {
                        dataGridView1.Rows[i].Cells[f.cmbColumnCombo.Text].Value = dataGridView1.Rows[i].Cells[f.cmbColumnCombo.Text].Value.ToString().Replace(f.txtfind.Text, f.txtreplace.Text);
                        #region Commented
                        //dataGridView1.Rows[i].Cells[f.cmbColumnCombo.Text].Value = dataGridView1.Rows[i].Cells[f.cmbColumnCombo.Text].Value.ToString().Replace(f.txtfind.Text, f.txtreplace.Text);
                        //bulidDataRow(i);
                        #endregion
                    }
                }
            }
        }

private void btnNext_Click(object sender, EventArgs e)
    {
        currentPage += 1;
        if (currentPage > PageCount)
        {
            currentPage = PageCount;
            //Check if you are already at the last page.
            if (recNo == maxRec)
            {
                MessageBox.Show("You are at the Last Page!");
                return;
            }
        }
        LoadPage(MyFOrmat);
    }
加载页面代码:

List<String> cmbList = new List<string>();
        public String Replace;
        public String Find;
        public String Col;
        public String NewColumn;
        public String NewColumnValue;
        public string MyFOrmat { get; set; }
        int PageCount;
        int maxRec;
        int pageSize = 30;
        int currentPage = 1;
        int recNo = 0;
        string FileName;
        String[] datfile;
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);
                }
            }
        }
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]);
                                    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('þ'); }
    index += 1;
                                    i += 1;
                                }
                            }
                            rowindex += 1;
                        }
                    }
                    recNo += 1;
                }
            }
 private void btnFindandReplace_Click(object sender, EventArgs e)
        {
            Form2 f = new Form2();
            f.cmbColumnCombo.DataSource = cmbList;
            f.ShowDialog();

            for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
            {
                //dataGridView1.Rows[rowindex].Cells[index].Value = Convert.ToDateTime(column[i]).ToString(Format);
                if (dataGridView1.Rows[i].Cells[f.cmbColumnCombo.Text].Value.ToString().ToLower().Contains(f.txtfind.Text.ToLower()))
                {
                    //dataGridView1.Rows[i].Cells[f.cmbColumnCombo.Text].Value = dataGridView1.Rows[i].Cells[f.cmbColumnCombo.Text].Value.ToString().ToLower().Replace(f.txtfind.Text.ToLower(), f.txtreplace.Text);
                    //bulidDataRow(i);
                    if (!string.IsNullOrEmpty(f.txtfind.Text))
                    {
                        dataGridView1.Rows[i].Cells[f.cmbColumnCombo.Text].Value = dataGridView1.Rows[i].Cells[f.cmbColumnCombo.Text].Value.ToString().Replace(f.txtfind.Text, f.txtreplace.Text);
                        #region Commented
                        //dataGridView1.Rows[i].Cells[f.cmbColumnCombo.Text].Value = dataGridView1.Rows[i].Cells[f.cmbColumnCombo.Text].Value.ToString().Replace(f.txtfind.Text, f.txtreplace.Text);
                        //bulidDataRow(i);
                        #endregion
                    }
                }
            }
        }

private void btnNext_Click(object sender, EventArgs e)
    {
        currentPage += 1;
        if (currentPage > PageCount)
        {
            currentPage = PageCount;
            //Check if you are already at the last page.
            if (recNo == maxRec)
            {
                MessageBox.Show("You are at the Last Page!");
                return;
            }
        }
        LoadPage(MyFOrmat);
    }
public void加载页(字符串格式,bool-isfindandplace=false)
{
int startRec;
int endRec;
如果(currentPage==PageCount)
{
endRec=maxRec;
}
其他的
{
endRec=页面大小*当前页面;
}
dataGridView1.Rows.Clear();
如果(recNo==0)
{
dataGridView1.Columns.Clear();
}
int rowindex=0;
startRec=recNo;

对于(int RowCount=startRec;RowCount)总结您的需求:

  • 您希望读取10k-500k记录的较大数据文件
  • 您希望在
    DataGridView
  • 您希望允许用户修改数据:

    • 用户可以合并
    • 用户可以对数据使用更改和替换
    • 日期和时间列可以是拆分的
  • 应保存可能修改的数据
在我看来,您有两种方法:

  • 缓存数据
  • 缓存操作
缓存操作是可行的,但显然在编码缓存和保持数据同步方面要麻烦得多

因此,缓存数据将是我的首选

以下是如何分解功能的示意图:

  • 一个函数,用于读取整个数据并将其加载到
    DataTable
  • 用于初始显示和显示特定页面的功能
  • 用于对行列表进行每个更改的函数
调用更改函数后,必须刷新当前页面显示

保持内存中的数据总量今天应该不是什么问题;我注意到您正在以字符串的形式读取
datFile
数组中的所有数据。将其读取到表中可以避免反复拆分

DataTable.DataRow
还提供了一些不错的属性,如
hasrerrors
RowState
。其
项可以有一个专用类型来帮助格式化

但是请注意,
DataRow
没有(真实的)构造函数;相反,它必须从
DataTable
创建,因此您必须首先从列创建一个构造函数

显示代码将使用一个
pageSize
和一个
currentFirstLine
变量;它可以清除行并将行添加到DGV中,或者您可以使用
DataTable
进行绑定解决方案,您无论如何都需要在表上或
BindingSource
上保存
DataRows
和过滤器

当然,您也可以使用自己的结构,可能是一个简单的
string[]
List
来保存行数据

如果您对缓存操作的想法感兴趣,可以创建一个
ChangeAction
类,该类包含:

  • 类型
  • 所需的参数,即列、更改和替换字符串等
然后在
列表中
存储它们,然后将它们应用到每个未更改的行。但是第一个问题是:您需要知道哪些行已更改,并且可能需要知道是否可以在不破坏数据的情况下应用两次
更改操作。。更多问题可能会或可能不会在以后出现,取决于正在查看您的数据和操作的详细信息

下面是如何使用类级别变量设置绑定的示例:

DataTable DT = new DataTable();
BindingSource BS = new BindingSource();
int pageSize = 0;
int firstLineVisible = 0;
填写表格后,可以绑定表格并设置初始文件管理器:

BS.DataSource = DT;
dataGridView1.DataSource = BS;

pageSize = (dataGridView1.ClientSize.Height - dataGridView1.ColumnHeadersHeight) 
          / dataGridView1.Rows[0].Height;
int l1 = firstLineVisible; int l2 = firstLineVisible + pageSize;
BS.Filter = "Nr >= " + l1  + " and Nr < " + l2;
将其添加到数据中同样简单:

for (int l = 1; l < lines.Count; l++)
{
    var p = (l + sep[0] + lines[l]).Split(sep, StringSplitOptions.None);
    DT.Rows.Add(p);
}

您应该在设置
过滤器后立即添加该行,以总结您的需求:

  • 您希望读取10k-500k记录的较大数据文件
  • 您希望在
    DataGridView
  • 您希望允许用户修改数据:

    • 用户可以合并
    • 用户可以对数据使用更改和替换
    • 日期和时间列可以是拆分的
  • 应保存可能修改的数据
在我看来,您有两种方法:

  • 缓存数据
  • 缓存操作
缓存操作是可行的,但显然在编码缓存和保持数据同步方面要麻烦得多

因此,缓存数据将是我的首选

以下是如何分解功能的示意图:

  • 一个函数,用于读取整个数据并将其加载到
    DataTable
  • 用于初始显示和显示特定页面的功能
  • 用于对行列表进行每个更改的函数
调用更改函数后,必须刷新当前页面显示