column1, 셀 중 하나가 비어 있지 않은지 확인하려고합니다. 그런 다음 비우고 파일을 다음 열 셀에 복사하고 싶습니다. 내 생각은 하나의 특정 Column1- 모든 셀이 "COLUMN1"셀 중 하나가 비어 있지 않은지 확인하는 것입니다. 그런 다음 다음 column2에 복사하려면 [특정 셀에 파일 경로를 첨부했습니다] 파일이 필요합니다. 동시에 파일을 데스크탑의 폴더에 복사하고 싶습니다. 위치가 C : / user / elec / copy이고 Column1 셀 데이터를 지우고 싶습니다.
어떻게 할 수 있습니까? 내가하려는 작업에 대한 링크 ..
편집 된 코딩 .....
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;
}
}
}
코드에 많은 문제가 있습니다. DataGridViewCell
값 대신 읽으려고 합니다. 셀 값을 읽으려면의 .Value
속성 에서 값을 반환해야합니다 DataGridVIewCell
. 즉. string x=dgv.Rows[0].Cells[1].Value
. 행에 셀이 포함 된 열이 없습니다. 따라서 특정 셀에서 값을 읽으려면 Column 대신 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[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);
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다