【问题标题】:How to copy the content from one cell to another如何将内容从一个单元格复制到另一个单元格
【发布时间】:2014-08-24 02:50:28
【问题描述】:

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

我该怎么做。链接我正在尝试做的事情..

编辑编码.....

 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;
                }
            }
    }

【问题讨论】:

  • 那么,您的实际问题是什么?
  • @JohnSaunders 我不知道该怎么做..请帮帮我..我想将一个单元格内容复制到另一列单元格内容..如何做到这一点..
  • 做一些研究。不要只是谷歌的例子。去学习 DataGridView 类。问题:DataGridView 的单元格的数据类型是什么?它是否有任何与剪贴板有关的属性或方法?
  • @Shell howzz going ...你能帮我解决这个问题吗..拜托了
  • @Shell 我也想将新位置和数据更新到数据库中。请帮帮我.. 这样我就可以关闭整个主题.. 非常感谢 Shell

标签: c# winforms datagridview


【解决方案1】:

你的代码有很多问题。您正在尝试阅读 DataGridViewCell 而不是它的值。要读取单元格值,您必须从DataGridVIewCell.Value 属性返回值。 IE。 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);

【讨论】:

  • 我对 Exist、create、Cell 有错误.. 然后行 string filePath = dataGridView1.Rows[i].Cells[0].Value; 和最后一行也有.. dataGridView1.row.Cells[0].Value = string.Empty;
  • 您没有提到要更新数据库中的记录。您需要创建另一个函数来将该记录保存在数据库中。顺便说一句,你如何填充网格中的记录?在您的问题中添加该代码。
  • 我有一些疑问。在您的代码中,您尝试从 column1path(textbox) 而不是网格单元格更新记录。以及在 datagridview 的哪一列中,您存储了该记录的 id?
  • @Stacy 我已更新代码以更新数据库中的该记录。
  • 我已经更新了我的答案@Stacy 请看一下。如果您对此有更多疑问,可以在这里找到我chat.stackoverflow.com/rooms/43899/asp-net-with-c-vb-mvc
猜你喜欢
  • 2012-08-04
  • 2018-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多