重要在添加新行之前检查数据绑定数据集 [英] Important Checking the databound dataset before a new row is added
问题描述
您好,我正在将应用程序从excel导入到datagridview.我的excel中的列作为行导入到datagridview.
例如,在Excel中Item1 Item2 Item 3可用
但是在datagridview中,我将其显示为
Item1
ITem2
Item3
经过很长一段时间,我已经成功地做到了.
我实际上是在导入完成之前(即在表单加载本身期间)将数据集绑定到datagridiview.
因此,在绑定到datagridview之前,我想检查数据集中是否已存在新添加的行.
用于例如
DataGridview
Item1
Item2
Item3
同样,如果Item2 Item3在添加新行时存在(即从excel导入)
以下是我到目前为止所做的事情
Hi in my application I''m importing from excel to datagridview. Columns in my excel is imported as rows to datagridview.
Eg in Excel Item1 Item2 Item 3 are available
But in datagridview I''m displaying it as
Item1
ITem2
Item3
I''ve done this successfully after a long run.
I''m actually binding the dataset to datagridiview before the import is done, ie during the form load itself.
So I want to check whether newly added row is already existing in the dataset before binding to datagridview a form of validation.
for Eg
DataGridview
Item1
Item2
Item3
Again if Item2 Item3 exists while adding new row (ie importing from excel)
Following is wat I''ve done so far
private void btImport_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel.Application excelApp;
Microsoft.Office.Interop.Excel.Workbook workbook;
Microsoft.Office.Interop.Excel.Worksheet worksheet;
Microsoft.Office.Interop.Excel.Range range;
excelApp = new Microsoft.Office.Interop.Excel.Application();
string filename = string.Empty;
var missing = System.Reflection.Missing.Value;
try
{
OpenFileDialog ofdGetFile = new OpenFileDialog();
ofdGetFile.Filter = "Excel files |*.xls|All files (*.*)|*.*";
ofdGetFile.FilterIndex = 2;
ofdGetFile.CheckFileExists = true;
ofdGetFile.Title = "Select an Excel File";
ofdGetFile.RestoreDirectory = true;
ofdGetFile.ShowDialog();
filename = ofdGetFile.FileName;
workbook = excelApp.Workbooks.Open(filename, 0, true, 5, "", "", true,
Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets.get_Item(1);
range = worksheet.UsedRange;
Array myValues = (Array)range.Cells.Value2;
int vertical = myValues.GetLength(0);
int horizontal = myValues.GetLength(1);
for (int i = 1; i <= horizontal; i++)
{
DataRow objdr = dsobbtemp.Tables[0].NewRow();
objdr["objectRevisedName"] = myValues.GetValue(1, i).ToString();// i;
dsobbtemp.Tables[0].Rows.Add(objdr);
}
dgDtParameters.DataSource = dsobbtemp.Tables[0];
workbook.Close(true,null,null);
excelApp.Quit();
releaseObject(workbook);
releaseObject(worksheet);
releaseObject(excelApp);
}
catch (Exception exGeneral)
{
MessageBox.Show(exGeneral.Message);
}
finally
{
GC.Collect();
}
}
我该怎么做.
有人帮我,真是伤脑筋
在此先感谢
How can I accomplish this.
Anyone help me,really breaking my heads
Thanks in advance
推荐答案
for (int i = 1; i <= horizontal; i++)
{
DataRow objdr = dsobbtemp.Tables[0].NewRow();
objdr["objectRevisedName"] = myValues.GetValue(1, i).ToString();// i;
if(i>1)
{
if( dsobbtemp.Tables[0].Rows[i-1]!=objdr)
{
dsobbtemp.Tables[0].Rows.Add(objdr);
}
}
else
{
dsobbtemp.Tables[0].Rows.Add(objdr);
}
}
这篇关于重要在添加新行之前检查数据绑定数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!