Thursday, March 24, 2011

Accessing the DataGridRow from WPF DataGrid



/***********************************************************************
/* Description: Accessing the DataGridRow from WPF DataGrid by row index */
***********************************************************************/




DataGridRow dgr = (DataGridRow)dataGrid1.ItemContainerGenerator.ContainerFromIndex(nRowIndex);
if (dgr == null) // row might be invisible (top / bottom). So, scroll into the view and get it
{
dataGrid1.UpdateLayout();
dataGrid1.ScrollIntoView(dataGrid1.Items[nRowIndex]);
dgr = (DataGridRow)dataGrid1.ItemContainerGenerator.ContainerFromIndex(nRowIndex);
}




Wednesday, March 23, 2011

Get All Sheet Names From Excel File with C# and ADO.NET



/***********************************************************************
Description:Dealing with excel files using ADO.NET is as good as dealing with ms-access databases. Here is the code for getting all the sheet names from the given excel file. Each worksheet can be assumed as a table in database. Change the extended properties in the connection string to deal with various verions of excel files.
This code is compiled in the environment C#, VS2010, .NET 4.0.
***********************************************************************/




public string[] GetExcelSheetNames(string excelFileName)
{
OleDbConnection con = null;
DataTable dt = null;
String conStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + excelFileName + ";Extended Properties=Excel 8.0;";
con = new OleDbConnection(conStr);
con.Open();
dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dt == null)
{
return null;
}
String[] excelSheetNames = new String[dt.Rows.Count];
int i = 0;
foreach (DataRow row in dt.Rows)
{
excelSheetNames[i] = row["TABLE_NAME"].ToString();
i++;
}
return excelSheetNames;
}