首页 新闻 赞助 找找看

一个页面导入数据信息之后跳转到另一个页面

0
悬赏园豆:5 [已解决问题] 解决于 2013-05-09 14:23

C#中点击一个userControl里的导入按钮得到的信息怎样插入到另一个userControl里的gridview中?

这是前后对照图:

点击导入之后是:

对话框可以不要

lichen辰的主页 lichen辰 | 初学一级 | 园豆:196
提问于:2013-04-10 17:13
< >
分享
最佳答案
0

usercontrol

收获园豆:5
zhi++ | 菜鸟二级 |园豆:487 | 2013-04-10 18:48
其他回答(2)
0

是在同一个页面吗?

不负春光,努力生长 | 园豆:1382 (小虾三级) | 2013-04-10 21:29

不在同一个页面

支持(0) 反对(0) lichen辰 | 园豆:196 (初学一级) | 2013-04-11 08:56

@lichen辰: 你可以尝试用一下委托,因为你是在不同的页面,要在A页面操作,响应B页面的事件,这个委托可以解决

支持(0) 反对(0) 不负春光,努力生长 | 园豆:1382 (小虾三级) | 2013-04-14 14:31
0

//添加按钮
private void btnAdd_Click(object sender, EventArgs e)
{
DialogResult result = openFileDialog.ShowDialog();
if (DialogResult.OK == result)
{
string filepath = openFileDialog.FileName;
FileInfo fileInfo = new FileInfo(filepath);
string fileName = fileInfo.Name;
string fileType = fileName.Split('.')[1];

//在表格里添加单元格
DataGridViewRow row = new DataGridViewRow();
row.CreateCells(dataGridView, new string[] { fileName.Split('.')[0], fileType, filepath });
this.dataGridView.Rows.Add(row);
}

}

//删除
private void btnDelete_Click(object sender, EventArgs e)
{
DataGridViewSelectedRowCollection rows = this.dataGridView.SelectedRows;
foreach (DataGridViewRow row in rows)
{
this.dataGridView.Rows.Remove(row);
}
}

//得到导入文件路径
public string[] getFilePath()
{
int dataCount = this.dataGridView.Rows.Count;
if (dataCount > 0)
{
string[] filePaths = new string[dataCount];
//选中行集合
DataGridViewSelectedRowCollection rows=this.dataGridView.SelectedRows;
//获取选中文件路径
for(int i=0;i<rows.Count;i++)
{
filePaths[i] = rows[i].Cells[2].Value.ToString() ;
}
return filePaths;
}
else
{
MessageDialog.ShowWarning("请添加导入文件!");
return null;
}

}

//导入按钮
private void btnImport_Click(object sender, EventArgs e)
{
string[] filepaths = addDataFile.getFilePath();
if (filepaths == null)
return;
try
{
foreach (string filepath in filepaths)
{
string sqlStr = "select * from " + Path.GetFileName(filepath);
dtTable = Tool.readCSVToDataTable(sqlStr,filepath);
dataToDabase(dtTable);
}
}
catch (Exception ex)
{
MessageDialog.ShowError(ex.Message);
}
}
#endregion


//导入数据到数据库
public void dataToDabase(DataTable dtTable)
{
int addCount = dtTable.Rows.Count;
string sqlSelect = "select * from " + talbe;
int deleteCount = DBObject.QueryCount(sqlSelect);
string sqlDeleteStr = "truncate table " + talbe;
DBObject.ExecuteSql(Tool.DB_NAME,sqlDeleteStr);
//添加
DataTableToDataBase(Tool.DB_CONNECT, talbe, dtTable);
DataImportFinish dataImportFinish = new DataImportFinish(addCount, deleteCount);
panel2.Controls.Clear();
panel2.Controls.Add(dataImportFinish);
btnImport.Hide();
btnOver.Visible = true;
}

#region 数据操作
/// <summary>
/// 将读取的数据放入到dataBase中
/// </summary>
/// <param name="connectionStr"></param>
/// <param name="tableName"></param>
/// <param name="dt"></param>
private void DataTableToDataBase(string connectionStr,string tableName,DataTable dt)
{
SqlBulkCopy sqlbulkcopy = new SqlBulkCopy(connectionStr, SqlBulkCopyOptions.UseInternalTransaction);
sqlbulkcopy.DestinationTableName = tableName;//数据库中的表名
sqlbulkcopy.WriteToServer(dt);
}
#endregion

lichen辰 | 园豆:196 (初学一级) | 2013-05-09 14:22
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册