Thursday, June 30, 2011

WPF C# DataGrid row background color




// do this in LoadingRow event
private void dataGrid1_LoadingRow(object sender, DataGridRowEventArgs e)
{
e.Row.Background = Brushes.Green; // set to background to green
}


Wednesday, June 29, 2011

WPF C# DataGrid Column Hide or Show





DataGridColumn col = dataGrid1.Columns[0]; // first column
col.Visibility = Visibility.Hidden; // hide
col.Visibility = Visibility.Visible; // show




Tuesday, June 28, 2011

Change background color of a dialog box in VC++ MFC





// .H file
class CMyDlg : public CDialogEx
{
COLORREF m_clrBk = RGB(255,255,200); // assign the color as per requirement
public:
afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
};



// .CPP file
BEGIN_MESSAGE_MAP(CMyDlg, CDialogEx)
ON_WM_CTLCOLOR()
END_MESSAGE_MAP()

HBRUSH CMyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
if(nCtlColor == CTLCOLOR_DLG) // remove if statement to change the background for all the controls
{
hbr = CreateSolidBrush(m_clrBk);
pDC->SetBkColor(m_clrBk);
}
return hbr;
}





Monday, June 27, 2011

WPF stop closing the window or cancelling from closing the window





void MyWPFWindow_Closing(object sender, CancelEventArgs e)
{
e.Cancel = true; // setting to true will not close the window
}




Sunday, June 26, 2011

WPF ShowDialog return value





void MyWPFWindow_Closing(object sender, CancelEventArgs e)
{
DialogResult = true; // or false based on your requirement
}

//Usage
MyWPFWindow wnd = new MyWPFWindow();
bool retvalue = wnd.ShowDialog();




Thursday, June 23, 2011

Mapping OleDbType with .Net Type



/***********************************************************************
/* Description: When dealing with excel files and it's data through .Net application it may require to compare the OLE datatypes(excel) and .net datatypes. Following code is compiled using VS2010 and C#.
***********************************************************************/




public static bool IsOleDBEqualToSystemType(TypeCode sysType, OleDbType oleType)
{
bool bRet = false;
switch (oleType)
{
case OleDbType.BigInt:
bRet = sysType == TypeCode.Int64;
break;
case OleDbType.Binary:
bRet = sysType == TypeCode.Byte;
break;
case OleDbType.Boolean:
bRet = sysType == TypeCode.Boolean;
break;
case OleDbType.BSTR:
bRet = sysType == TypeCode.String;
break;
case OleDbType.Char:
bRet = sysType == TypeCode.String;
break;
case OleDbType.Currency:
bRet = sysType == TypeCode.Decimal;
break;
case OleDbType.Date:
bRet = sysType == TypeCode.DateTime;
break;
case OleDbType.DBDate:
bRet = sysType == TypeCode.DateTime;
break;
case OleDbType.DBTime:
bRet = sysType == TypeCode.DateTime;
break;
case OleDbType.DBTimeStamp:
bRet = sysType == TypeCode.DateTime;
break;
case OleDbType.Decimal:
bRet = sysType == TypeCode.Decimal;
break;
case OleDbType.Double:
bRet = sysType == TypeCode.Double;
break;
case OleDbType.Integer:
bRet = sysType == TypeCode.Int32;
break;
case OleDbType.LongVarChar:
bRet = sysType == TypeCode.String;
break;
case OleDbType.LongVarWChar:
bRet = sysType == TypeCode.String;
break;
case OleDbType.Numeric:
bRet = sysType == TypeCode.Decimal;
break;
case OleDbType.Single:
bRet = sysType == TypeCode.Single;
break;
case OleDbType.SmallInt:
bRet = sysType == TypeCode.UInt16;
break;
case OleDbType.TinyInt:
bRet = sysType == TypeCode.SByte;
break;
case OleDbType.UnsignedBigInt:
bRet = sysType == TypeCode.UInt64;
break;
case OleDbType.UnsignedInt:
bRet = sysType == TypeCode.UInt32;
break;
case OleDbType.UnsignedSmallInt:
bRet = sysType == TypeCode.UInt16;
break;
case OleDbType.UnsignedTinyInt:
bRet = sysType == TypeCode.Byte;
break;
case OleDbType.VarChar:
bRet = sysType == TypeCode.String;
break;
case OleDbType.VarNumeric:
bRet = sysType == TypeCode.Decimal;
break;
case OleDbType.VarWChar:
bRet = sysType == TypeCode.String;
break;
case OleDbType.WChar:
bRet = sysType == TypeCode.String;
break;
}
return bRet;
}

Follow the link "http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbtype(v=vs.71).aspx" for more details





Sunday, June 19, 2011

Get Column Names And Data Types from the Database without reading the data



/***********************************************************************
/* Description: Extracting column details from the excel book is as good as reading the details from MS-Access MDB file. Following code demonstrates the procedure. Same procedure can be followed for MDB file also just by changing the connection string.
This code is compiled in VS2010, C#.
***********************************************************************/




OleDbConnection conn = new OleDbConnection("Provider= Microsoft.Jet. OLEDB.4.0;Data Source=" + txtFileName.Text + ";Extended Properties=Excel 8.0");
string strSheetName = GetExcelSheetNames(txtFileName.Text)[0];
conn.Open();
DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns,
new object[] { null, null, strSheetName, null });

OleDbType[] exColTypes = new OleDbType[dt.Rows.Count];
string[] exColNames = new string[dt.Rows.Count];

foreach (DataRow row in dt.Rows)
{
int nColIndex = Convert.ToInt32(row[6]);
exColTypes[nColIndex - 1] = (OleDbType)row[11];
exColNames[nColIndex - 1] = row[3].ToString();
}

conn.Close();