【问题标题】:DataGridView issuesDataGridView 问题
【发布时间】:2010-07-08 05:21:29
【问题描述】:

我正在尝试遍历我的 foreach 循环并将输出输出到我的 datagridview。

datagridview 有 3 个定义的列:路径、旧文件名、新文件名

但是,我总是遇到错误。

首先,代码如下:

public partial class SanitizeFileNames : Form
{

    public SanitizeFileNames()
    {
        InitializeComponent();
    }

    public void Sanitizer(List<string> paths)
    {
    string regPattern = (@"[~#&!%+{}]+");
        string replacement = "";

        Regex regExPattern = new Regex(regPattern);
        List<string> existingNames = new List<string>();

        StreamWriter errors = new StreamWriter(@"C:\Documents and Settings\bob.smith\Desktop\SharePointTesting\Errors.txt", true);
        StreamWriter resultsofRename = new StreamWriter(@"C:\Documents and Settings\bob.smith\Desktop\SharePointTesting\Results of File Rename.txt", true);

        dataGridView1.Rows.Clear();
        var filesCount = new Dictionary<string, int>();


           try
            {

              foreach (string files2 in paths)
              {

                string filenameOnly = Path.GetFileName(files2);
                string pathOnly = Path.GetDirectoryName(files2);
                string sanitizedFileName = regExPattern.Replace(filenameOnly, replacement);
                string sanitized = Path.Combine(pathOnly, sanitizedFileName);


                if (!System.IO.File.Exists(sanitized))
                {
                    System.IO.File.Move(files2, sanitized);
                    DataGridViewRow dgr2 = new DataGridViewRow();
                    dgr2.Cells[0].Value = pathOnly;
                    dgr2.Cells[1].Value = filenameOnly;
                    dgr2.Cells[2].Value = sanitized;
                    resultsofRename.Write("Path: " + pathOnly + "\r\n" + "Old File Name: " + filenameOnly + "\r\n" + "New File Name: " + sanitized + "\r\n" + "\r\n");

                }

                else
                {
                    if (filesCount.ContainsKey(sanitized))
                    {
                        filesCount[sanitized]++;
                    }
                    else
                    {
                        filesCount.Add(sanitized, 1);
                    }
                    string newFileName = String.Format("{0}{1}{2}",
                    Path.GetFileNameWithoutExtension(sanitized),
                    filesCount[sanitized].ToString(),
                    Path.GetExtension(sanitized));
                    string newFilePath = Path.Combine(Path.GetDirectoryName(sanitized), newFileName);
                    System.IO.File.Move(files2, newFilePath);
                    DataGridViewRow dgr2 = new DataGridViewRow();
                    dgr2.Cells[0].Value = pathOnly;
                    dgr2.Cells[1].Value = filenameOnly;
                    dgr2.Cells[2].Value = newFileName;
                    resultsofRename.Write("Path: " + pathOnly + "\r\n" + "Old File Name: " + Path.GetFileNameWithoutExtension(files2) + "\r\n" + "New File Name: " + newFileName + "\r\n" + "\r\n");
                }
               }
              }
            catch (Exception e)
            {

             errors.Write(e);

            }





    }

    private void SanitizeFileNames_Load(object sender, EventArgs e)
    {

    }

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {

    }

}

首先,我是 C# 新手,所以我什至不能 100% 确定这是正确的。这段代码所做的是获取从另一个方法传入的路径,并通过删除无效字符(如我的正则表达式模式中所定义)来“清理”文件名。如果文件名已经存在,请在文件名后面附加一个递增的数字(即 ~test.txt 和 %test.txt 都将重命名为 test.txt。使用我的代码,~test.txt 变为 test.txt 和 % test.txt 变为 test1.txt)。 我基本上是在尝试将此数据附加到我的 datagridview(文件是否应该转到 else if(filesCount.ContainsKey(sanitized) 循环)。

Path.GetFileNameWithoutExtension()、Path.GetExtension()、Path.Combine()、Path.GetDIrectoryName() 出现错误。错误如下:

'System.Windows.Forms.DataGridViewTextBoxColumn' 不包含定义 'GetFileName' 并且没有扩展方法 'GetFileName' 接受第一个 类型参数 'System.Windows.Forms.DataGridViewTextBoxColumn' 可以找到(您是否缺少 使用指令或程序集 参考?)

我不知道这意味着什么或如何解决这个问题。有人可以帮忙吗?

【问题讨论】:

  • 根据上面的代码,我看不出您是如何得到该错误的。但是让我问你,你需要使用数据网格有什么原因吗?如果您要手动生成行和列而不绑定到任何东西,那么使用 可能会更简单。如果您认为这可行,我可以为您提供一些示例代码。
  • 从您发布的代码中我看不到编译错误在哪里。你能指出你得到错误的行吗?

标签: c# winforms datagridview


【解决方案1】:

我怀疑您在某处有一个名为“路径”的 DataGridViewTextBoxColumn 变量/字段。重命名该变量和/或使用全名 System.IO.Path.GetFileName() 等。

作为更一般的评论,停止使用 DatagridView 作为数据结构。帮自己一个忙,用你想要的(列)属性定义一个类,声明一个BindingList&lt;MyFileClass&gt; 并将 DataGridView 绑定到该列表。

【讨论】:

  • 我认为那是System.IO.Path.GetFileName() 另外,++ 有一个名为 Path 的列。这几乎 100% 保证发生了什么......
【解决方案2】:

例子:

public ArrayList GetSelectedRows(DataGrid datagrid)
{
ArrayList arrSelectedRows = new ArrayList();
DataSet dset = (DataSet)datagrid.DataSource;
for (int i=0; i<dset.Tables[0].Rows.Count; i++)
{
if (datagrid.IsSelected(i))
{
DataRow drow = dset.Tables[0].Rows[i];
arrSelectedRows.Add(drow);
}
}
return arrSelectedRows;
}

如果你想去的话,这应该可以让你...不过,显然也不完全像你所追求的那样。

This should get you were you want to go...though, obviously not quite like what you were after either.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-01
    相关资源
    最近更新 更多