如何将内容从一个单元格复制到另一个单元格

时间:2014-08-24 02:50:29

标签: c# winforms datagridview

我试着检查是否有任何column1,任何一个单元格都不为空。我想制作然后为空并将文件复制到下一个列单元格。什么,我的想法是检查一个特定的Column1-所有单元格是否可以说" COLUMN1"其中一个单元格不是空的。然后我需要文件[我已经附加到该特定单元格的文件路径]以复制到下一个column2。同时我想将文件复制到桌面上的文件夹让我们说位置是C:/ user / elec / copy,我想要删除Column1 -cell数据。

我该怎么办?链接我正在尝试做什么..

enter image description here

编辑编码.....

 private void button6_Click(object sender, EventArgs e)
    {
            string copyPath = @"C:\user\elec\copy";
            if (!System.IO.Directory.Exists(copyPath))
                System.IO.Directory.CreateDirectory(copyPath);
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (dataGridView1.Rows[i].Cells[2].Value != null && !String.IsNullOrEmpty(dataGridView1.Rows[i].Cells[2].Value.ToString()))
                {
                    string filePath = dataGridView1.Rows[i].Cells[2].Value.ToString();
                    if (System.IO.File.Exists(filePath))
                    {
                        string fileName = System.IO.Path.GetFileName(filePath);
                        string newpath = System.IO.Path.Combine(copyPath, fileName);
                        System.IO.File.Copy(filePath, newpath, true);
                        dataGridView1.Rows[i].Cells[3].Value = newpath;

                        try
                        {
                            SqlConnection con = new SqlConnection(@"Data Source=SREEJITHMOHA492\SQLEXPRESS;Initial Catalog=cndb;Integrated Security=True");
                            {
                                con.Open();
                                SqlCommand cmd = con.CreateCommand();
                                cmd.CommandText = "update cncinfo set draftpath=@draftpath,releasepath=@releasepath Where part=@part";
                                cmd.Parameters.Add("@draftpath", SqlDbType.NVarChar).Value = filePath;
                                cmd.Parameters.Add("@releasepath", SqlDbType.NVarChar).Value = newpath;
                                cmd.CommandText = "update cncinfo set draftpath='" + string.Empty + "',releasepath=@releasepath Where part=@part";
                                //you must have the id value in datagridview to update the specific record.
                                cmd.Parameters.Add("@part", SqlDbType.NVarChar).Value = Convert.ToString(dataGridView1.Rows[i].Cells["Part Number"].Value);
                                cmd.ExecuteNonQuery();
                                con.Close();
                            }
                        }
                        catch (System.Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }
                    dataGridView1.Rows[i].Cells[2].Value = string.Empty;
                }
            }
    }

1 个答案:

答案 0 :(得分:0)

您的代码中存在很多问题。您正在尝试阅读DataGridViewCell而不是其值。要读取必须从.Value DataGridVIewCell属性返回值的单元格值。即。 string x=dgv.Rows[0].Cells[1].Value。行不包含它包含单元格的列。因此,要从特定单元格中读取值,您应该从Cell对象而不是Column中读取该值。

private void button6_Click(object sender, EventArgs e)
{
    string copyPath = @"C:\user\elec\copy";
    if (!System.IO.Directory.Exists(copyPath))
        System.IO.Directory.CreateDirectory(copyPath);
    for (int i = 0; i < dataGridView1.Rows.Count; i++)
    {
        if (dataGridView1.Rows[i].Cells[0].Value != null && 
            !String.IsNullOrEmpty(dataGridView1.Rows[i].Cells[0].Value.ToString()))
        {
            string filePath = dataGridView1.Rows[i].Cells[0].Value.ToString();
            if (System.IO.File.Exists(filePath))
            {                   
                string fileName = System.IO.Path.GetFileName(filePath);
                string newpath = System.IO.Path.Combine(copyPath, fileName);
                System.IO.File.Copy(filePath, newpath, true);
                dataGridView1.Rows[i].Cells[1].Value = newpath;

                try
                { 
                    Using (SqlConnection con = new SqlConnection(@"Data Source=STACY121\SQLEXPRESS;Initial Catalog=cndb;Integrated Security=True"))
                    {
                        con.Open();
                        SqlCommand cmd = con.CreateCommand();
                        cmd.CommandText = "update cncinfo set Column1=@Column1,Column2=@Column2 Where id=@id";
                        cmd.Parameters.Add("@Column1",SqlDbType.VarChar).Value =filePath;
                        cmd.Parameters.Add("@Column2",SqlDbType.VarChar).Value =newpath;
                        //you must have the id value in datagridview to update the specific record.
                        cmd.Parameters.Add("@id",SqlDbType.Int).Value =Convert.ToInt32(dataGridView1.Rows[i].Cells["id"].Value);


                        cmd.ExecuteNonQuery();
                        con.Close();
                    }
                 }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                } 
            }
            dataGridView1.Rows[i].Cells[0].Value = string.Empty;
        }
    }
}

<强>编辑:

如果您想使用不同的文件名进行复制,则可以像这样指定文件名。

int iFileNo = 1; // this is the part of file number in sample-rev-01
string fileName = System.IO.Path.GetFileName(filePath);
string ext = new System.IO.FileInfo(filePath).Extension;
//if the file name is sample.txt then it would be return as sample-rev-01.txt
fileName = String.Format("{0}-rev-{1:00}{2}", fileName.Replace(ext, ""), iFileNo, ext);

string newpath = System.IO.Path.Combine(copyPath, fileName);
System.IO.File.Copy(filePath, newpath, true);