Delegate.Invoke: Executes synchronously, on the same thread.
Delegate.BeginInvoke: Executes asynchronously, on a threadpool thread.
Control.Invoke: Executes on the UI thread, but calling thread waits for completion before continuing.
Control.BeginInvoke: Executes on the UI thread, and calling thread doesn't wait for completion.
Use Code from link http://www.codeproject.com/KB/cs/AvoidingInvokeRequired.aspx
static public class UIHelper
{
static public void UIThread(Control control, MethodInvoker code)
{
if (control.InvokeRequired)
{
control.BeginInvoke(code);
return;
}
control.Invoke();
}
}And then invoke the UIThread like this:
Collapse
Copy Code
UIHelper.UIThread(this, delegate
{
textBoxOut.Text = "New text";
});
Or
private delegate void DialogMessage_Delegate(string message);
private bool DialogMessage(string message)
{
if (InvokeRequired)
{
BeginInvoke(new DialogMessage_Delegate(TextMessages), new object[] { message });
}
else
{
if (System.Windows.Forms.MessageBox.Show(message,"Excel Template", MessageBoxButtons.OKCancel)
!= System.Windows.Forms.DialogResult.OK)
return false;
}
return true;
}
Friday, February 26, 2010
Reading data from Excel and create a datatable
//read data from Excel
object[,] colDataValues = (object[,])objRange.Value2;
//create columns
for (int index = 1; index <= colDataValues.GetLength(1); index++ )
{
DataColumn col = new DataColumn();
col.ColumnName = (string)colDataValues[1,index];
excelData.Columns.Add(col);
}
//reading rows values
for (int i = 2; i <= colDataValues.GetLength(0); i++)
{
DataRow row = excelData.NewRow();
for (int j = 1; j <= colDataValues.GetLength(1); j++)
{
row[j-1] = Convert.ToString(colDataValues[i, j]);
}
excelData.Rows.Add(row);
}
object[,] colDataValues = (object[,])objRange.Value2;
//create columns
for (int index = 1; index <= colDataValues.GetLength(1); index++ )
{
DataColumn col = new DataColumn();
col.ColumnName = (string)colDataValues[1,index];
excelData.Columns.Add(col);
}
//reading rows values
for (int i = 2; i <= colDataValues.GetLength(0); i++)
{
DataRow row = excelData.NewRow();
for (int j = 1; j <= colDataValues.GetLength(1); j++)
{
row[j-1] = Convert.ToString(colDataValues[i, j]);
}
excelData.Rows.Add(row);
}
Thursday, February 25, 2010
DataTable Notes
//select distinct
DataTable distinctTests = new DataTable();
distinctTests = ExistingDataTable.DefaultView.ToTable(true, columnName);
//select query and import row
DataTable oneUnit = tableGrid.Clone();
string queryStr = string.Format(NAME
+ "= '{0}' ", (string)dr[NAME]);
DataRow[] drsForOneUnit = tableGrid.Select(queryStr);
foreach (DataRow row in drsForOneUnit)
{
oneUnit.ImportRow(row);
}
SaveDataForEachUnit(oneUnit);
//set primary key
DataTable uniqueTestNFromExcel = excelData.DefaultView.ToTable(true, NAME);
DataColumn[] key = new DataColumn[1];
key[0] = uniqueTestNFromExcel.Columns[0];
uniqueTestNFromExcel.PrimaryKey = key;
DataTable distinctTests = new DataTable();
distinctTests = ExistingDataTable.DefaultView.ToTable(true, columnName);
//select query and import row
DataTable oneUnit = tableGrid.Clone();
string queryStr = string.Format(NAME
+ "= '{0}' ", (string)dr[NAME]);
DataRow[] drsForOneUnit = tableGrid.Select(queryStr);
foreach (DataRow row in drsForOneUnit)
{
oneUnit.ImportRow(row);
}
SaveDataForEachUnit(oneUnit);
//set primary key
DataTable uniqueTestNFromExcel = excelData.DefaultView.ToTable(true, NAME);
DataColumn[] key = new DataColumn[1];
key[0] = uniqueTestNFromExcel.Columns[0];
uniqueTestNFromExcel.PrimaryKey = key;
Wednesday, February 17, 2010
Excel (Record macros to learn how to use Excel in code)
If need to write code to Excel, can always record macros and get the code to use
Friday, February 5, 2010
User-Defined Table Types
To create a user-defined table type, use the CREATE TYPE statement. To ensure that the data in a user-defined table type meets specific requirements, you can create unique constraints and primary keys on the user-defined table type.
When use a user-defined table type as an argument, it's READ ONLY
When use a user-defined table type as an argument, it's READ ONLY
Subscribe to:
Posts (Atom)