C# 在c中手动添加行时如何过滤datagridview

C# 在c中手动添加行时如何过滤datagridview,c#,winforms,C#,Winforms,手动添加行时,如何使用文本框筛选行 这是我的密码: int n = XML_Grid.Rows.Add(); XML_Grid.ClearSelection(); XML_Grid.Rows[n].Cells[1].Value = FileName; XML_Grid.Rows[n].Cells[2].Value = Name; XML_Grid.Rows[n].Cells[3].Value = ID; XML_Grid.Rows[n].Cells[4].Value = Date; 如果无法筛

手动添加行时,如何使用文本框筛选行

这是我的密码:

int n = XML_Grid.Rows.Add();
XML_Grid.ClearSelection();
XML_Grid.Rows[n].Cells[1].Value = FileName;
XML_Grid.Rows[n].Cells[2].Value = Name;
XML_Grid.Rows[n].Cells[3].Value = ID;
XML_Grid.Rows[n].Cells[4].Value = Date;
如果无法筛选,如何使用datatable添加行,就像使用DataGridView一样

注意:这个问题可能是重复的,但我没有找到任何解决问题的方法

将Xml数据加载到DataTable,然后将此DataTable设置为Xml_Grid.DataSource

编辑:

如果要将行添加到通过从datagridview添加列添加的现有列中,则只需将XML\U Grid的每列的DataPropertyName设置为DataTable的列名,如

其中0,1,2,3是列的索引。验证与XML_网格中现有列对应的列索引

将上述代码行添加到XML_Grid.Rows.Clear的正上方

上述代码的替代方法是,您可以从datagridview的属性窗口为每列设置DataPropertyName

选择XML\u Grid==>打开属性窗口==>选择列属性==>选择列==>并将其DataPropertyName从none设置为FileName


所有剩余的列都是一样的。

到目前为止,从textbox中获得过滤数据的u代码是什么?因此,我们可以提供精确的解决方案xml_Grid.DefaultView.RowFilter=string.FormatFileName,比如“{0}%”,txt_FileName.Text;我试过了,但是datagridview不包含DefaultView的定义。如果我使用DataTable,这段代码可以工作。你是否使用了Windows窗体应用程序或WPF?已经在Tagsry中提到winforms这个XML_Grid.DataSource作为DataTable.DefaultView.RowFilter=string.FormatFileName,比如“{0}%”,txt_FileName.Text;让我know@Usama,查看答案可能对您有帮助:+1答案很有用,但我已经尝试过这段代码,它工作正常我不想按代码添加列我已经在datagridview中添加了列如何将行添加到我通过从datagridview添加列添加的现有列?@Usama,查看答案中的编辑部分是否有帮助:并让我知道。列文件名不属于表。不工作您是否删除了这些代码dt.Columns.AddFileName;dt.Columns.AddName;dt.Columns.AddID;dt.Columns.AddDate;?
DataTable dt = new DataTable();
dt.Clear();
dt.Columns.Add("FileName");
dt.Columns.Add("Name");
dt.Columns.Add("ID");
dt.Columns.Add("Date");

XML_Grid.Rows.Clear();
lbl_Path.Text = fbd.SelectedPath;
string[] files = Directory.GetFiles(fbd.SelectedPath, "*.xml");

XmlDocument doc = new XmlDocument();
XmlNodeList nodes = doc.GetElementsByTagName("cfdi:Emisor");
XmlNodeList nodes1 = doc.GetElementsByTagName("cfdi:Comprobante");

foreach (string tot_file in files)
{
    doc.Load(tot_file);
    string FileName = Path.GetFileNameWithoutExtension(tot_file);

    for (int i = 0; i < nodes.Count; i++)
    {
        string Name = nodes[i].Attributes["Nombre"].Value;
        string ID = nodes[i].Attributes["Rfc"].Value;
        string Date = nodes1[i].Attributes["Fecha"].Value;

        DataRow row = dt.NewRow();
        row["FileName"] = FileName;
        row["Name"] = Name;
        row["ID"] = ID;
        row["Date"] = Date;
        dt.Rows.Add(row);
    }
}

XML_Grid.DataSource = dt;
(XML_Grid.DataSource as DataTable).DefaultView.RowFilter = string.Format("FileName LIKE '{0}%'", txt_FileName.Text)
XML_Grid.Columns[0].DataPropertyName = "FileName";
XML_Grid.Columns[1].DataPropertyName = "Name";
XML_Grid.Columns[2].DataPropertyName = "ID";
XML_Grid.Columns[3].DataPropertyName = "Date";