【问题标题】:Inserting MatchCollection into datagridview c#将 MatchCollection 插入 datagridview c#
【发布时间】:2011-10-17 22:40:50
【问题描述】:

我有一个数据网格视图,它有 2 列和一个 MatchCollection,我将使用它来填充数据网格。如何在第一列的数据网格中插入 matchcollection 中的第一个匹配项,然后为第二列插入 matchcollection 的第二个值。然后它将创建一个新行并重新开始。

没有数据绑定到此网格视图,我需要确保插入数据网格的 matchCollection 不会覆盖表中的任何其他内容。我怎样才能做到这一点?

这是在表单应用程序而不是 asp.net 中完成的

 while (!sr.EndOfStream)
                    {
                        string line = sr.ReadLine();
                        line = line.Trim();
                        if (line.StartsWith("addTestingPageContentText"))
                        {
                            string temp;
                            string pattern = "\"([^\"]+)\"";
                            Regex r = new Regex(pattern);
                            MatchCollection regs = r.Matches(line);

                            foreach (Match reg in regs)
                            {

                                temp = reg.ToString();
                                temp = temp.Replace("\"", "");

                               int rowCount = contentTable_grd.Rows.Count - 1;


                        if (contentTable_grd.Rows[rowCount].Cells[0].Value == null)
                            contentTable_grd.Rows[rowCount].Cells[0].Value = temp;
                        else
                            contentTable_grd.Rows[rowCount].Cells[1].Value = temp;


                        contentTable_grd.Rows.Add();

                            }
                        }
                    }         

【问题讨论】:

    标签: c# winforms datagrid datagridview


    【解决方案1】:

    试试这个

    while (!sr.EndOfStream)
                {
                    string line = sr.ReadLine();
                    line = line.Trim();
                    if (line.StartsWith("addTestingPageContentText"))
                    {
                        string temp;
                        string pattern = "\"([^\"]+)\"";
                        Regex r = new Regex(pattern);
                        MatchCollection regs = r.Matches(line);
    
    
                        object[] array1 = new object[2];                    
    
    
    
                        foreach (Match reg in regs)
                        {
    
                            temp = reg.ToString();
                            temp = temp.Replace("\"", "");
    
                            if (array1[0] == null)
                                array1[0] = temp;
                            else
                                array1[1] = temp;
                        }
    
                        if (regs.Count > 0)
                            contentTable_grd.Rows.Add(array1);
                    }
                }
    

    【讨论】:

      【解决方案2】:

      您可以创建自定义 gridview 列并从 DataGridViewTextBoxCell 继承。

      Host Controls in Windows Forms DataGridView Cells

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-09-04
        • 1970-01-01
        • 2011-10-08
        • 1970-01-01
        • 1970-01-01
        • 2020-07-05
        • 1970-01-01
        • 2020-03-30
        相关资源
        最近更新 更多