如何在gridview中反映我当前的文件数量 [英] How to reflect my current number of files in my gridview

查看:72
本文介绍了如何在gridview中反映我当前的文件数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一些附加在一列中的文件移动到另一列,因此一旦文件被移动,我发现的记录总数不会更新,但我的网格会更新文件数。我该怎么办?



代码:

  public   void  move()
{
for (< span class =code-keyword> int i = 0 ; i < dataGridView1 .Rows.Count; i ++)
{
string fName1 = System.IO.Path.GetFileNameWithoutExtension(dataGridView1.Rows [i] .Cells [ 草稿路径]。Value.ToString());
string fName2 = System.IO.Path.GetFileNameWithoutExtension(dataGridView1.Rows [i] .Cells [ Release Path]。Value.ToString());

if (dataGridView1.Rows [i] .Cells [ 3 ]。值!= null &&!String.IsNullOrEmpty(dataGridView1.Rows [i] .Cells [ 草稿路径]。Value.ToString())&&
(dataGridView1.Rows [i] .Cells [ 4 ]。值== null || 字符串 .IsNullOrEmpty(dataGridView1 .Rows [i] .Cells [ Release Path]。Value.ToString()))& ;&
(dataGridView1.Rows [i] .Cells [ 0 ]。值!= null
&&( bool )dataGridView1.Rows [i] .Cells [ 0 ]。价值))
{

string filePath = dataGridView1.Rows [i] .Cells [ 草稿路径< /跨度>] Value.ToString();
string newFile = dataGridView1.Rows [i] .Cells [ Release Path]。Value.ToString();
string newFile1 = dataGridView1.Rows [i] .Cells [ 归档] Value.ToString();
string fileName = System.IO.Path.GetFileName(filePath);

if (newFile!= string .Empty&& fileName。等于(Path.GetFileName(NEWFILE),StringComparison.OrdinalIgnoreCase))
{
dataGridView1.Rows [I] .Cells [<跨度类= 代码串> <跨度类=code-string>错误
]。值= 发布时重复: + fileName + ;
dataGridView1.Rows [i] .Cells [ 错误] .Style = new DataGridViewCellStyle {ForeColor = Color.Red};
继续;
}
if (newFile1!= string .Empty&& fileName .Equals(Path.GetFileName(newFile1),StringComparison.OrdinalIgnoreCase))
{
dataGridView1.Rows [I] .Cells [<跨度类= 代码串> <跨度class =code-string>错误
]。值= 存档中重复: + fileName + ;
dataGridView1.Rows [i] .Cells [ 错误] .Style = new DataGridViewCellStyle {ForeColor = Color.Red};
继续;
}

string newpath = System.IO.Path.Combine(copyPath,fileName);

System.IO.File.Move(filePath,newpath);
dataGridView1.Rows [i] .Cells [ Release Path]。Value = newpath ;
dataGridView1.Rows [i] .Cells [ 草稿路径]。Value = < span class =code-keyword> string .Empty;
dataGridView1.Rows [i] .Cells [ 0 ]。值= false ; // 复选框


}
}
}

解决方案

我认为你最好创建一个DataTable [ ^ ]包含您需要的列,然后用数据填充DataTable。



如果您愿意,可以使用 LINQ [ ^ ]通过IQueryable< t>用于从数据表中提取数据的接口。

即使您可以将DataTable直接连接到DataGridView,最好使用BindingSource [ ^ ]因为必要时可以轻松过滤。



最后使用绑定源作为DataGridView的数据源。



直接操作DataGridView有点棘手,你已经发现了。



示例:

 DataTable myTable =  new  DataTable(  ATableName); 
myTable.Columns.Add( Column1 typeof string ));
myTable.Columns.Add( Column2 typeof int ));
myTable.Columns.Add( Column3 typeof bool ));

BindingSource bs = new BindingSource();
bs.DataSource = myTable;

dataGridView1.DataSource = bs;

myTable.Rows.Add( Row1 1 true );
myTable.Rows.Add( Row2 2 true );
myTable.Rows.Add( Row3 3 false );
myTable.AcceptChanges();





您还可以创建 DataSet [ ^ ]如果你有多个表可以运行。

在Visual Studio中使用向导创建一个DataSet需要更多的工作才能开始,但与 DataGridView [ ^ ]。


I am moving some files which are attached in one column to another column, So once the files are moved my total number of records found is not getting updated, but my grid is updated with number of files. How can i do this??

Codes:

public void move()
 {
for (int i = 0; i < dataGridView1.Rows.Count; i++)
        {
            string fName1 = System.IO.Path.GetFileNameWithoutExtension(dataGridView1.Rows[i].Cells["Draft Path"].Value.ToString());
            string fName2 = System.IO.Path.GetFileNameWithoutExtension(dataGridView1.Rows[i].Cells["Release Path"].Value.ToString());

            if (dataGridView1.Rows[i].Cells[3].Value != null && !String.IsNullOrEmpty(dataGridView1.Rows[i].Cells["Draft Path"].Value.ToString()) &&
   (dataGridView1.Rows[i].Cells[4].Value == null || String.IsNullOrEmpty(dataGridView1.Rows[i].Cells["Release Path"].Value.ToString())) &&
                (dataGridView1.Rows[i].Cells[0].Value != null
                && (bool)dataGridView1.Rows[i].Cells[0].Value))
            {

                string filePath = dataGridView1.Rows[i].Cells["Draft Path"].Value.ToString();
                string newFile = dataGridView1.Rows[i].Cells["Release Path"].Value.ToString();
                string newFile1 = dataGridView1.Rows[i].Cells["Archive"].Value.ToString();
                string fileName = System.IO.Path.GetFileName(filePath);

                if (newFile != string.Empty && fileName.Equals(Path.GetFileName(newFile), StringComparison.OrdinalIgnoreCase))
                {
                    dataGridView1.Rows[i].Cells["Error"].Value = "Duplicate in Release: " + fileName + "";
                    dataGridView1.Rows[i].Cells["Error"].Style = new DataGridViewCellStyle { ForeColor = Color.Red };
                    continue;
                }
                if (newFile1 != string.Empty && fileName.Equals(Path.GetFileName(newFile1), StringComparison.OrdinalIgnoreCase))
                {
                    dataGridView1.Rows[i].Cells["Error"].Value = "Duplicate in Archive: " + fileName + "";
                    dataGridView1.Rows[i].Cells["Error"].Style = new DataGridViewCellStyle { ForeColor = Color.Red };
                    continue;
                }

                string newpath = System.IO.Path.Combine(copyPath, fileName);

                System.IO.File.Move(filePath, newpath);
                dataGridView1.Rows[i].Cells["Release Path"].Value = newpath;
                dataGridView1.Rows[i].Cells["Draft Path"].Value = string.Empty;
                dataGridView1.Rows[i].Cells[0].Value = false; //Checkbox


            }
       }
   }

解决方案

I think you are better off creating a DataTable[^] with the columns you need then you fill the DataTable with your data.

If you want you can use LINQ[^] via the IQueryable<t> interface to extract data from the data table.
Even though you can connect the DataTable directly to the DataGridView, it is good practice to use a BindingSource[^] as it can be easily filtered if necessary.

At last use the binding source as the data source of the DataGridView.

Directly manipulate a DataGridView is kind of tricky, which you already have discovered.

Example:

DataTable myTable = new DataTable("ATableName");
myTable.Columns.Add("Column1", typeof(string));
myTable.Columns.Add("Column2", typeof(int));
myTable.Columns.Add("Column3", typeof(bool));

BindingSource bs = new BindingSource();
bs.DataSource = myTable;

dataGridView1.DataSource = bs;

myTable.Rows.Add("Row1", 1, true);
myTable.Rows.Add("Row2", 2, true);
myTable.Rows.Add("Row3", 3, false);
myTable.AcceptChanges();



You can also create a DataSet[^] if you have more than one table to operate.
Creating a DataSet with the wizard in Visual Studio is a bit of more work to get started, but gives better integration with the DataGridView[^].


这篇关于如何在gridview中反映我当前的文件数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆