所以我想要做的是调用一个方法,应该看起来像这样:
this.Invoke(new MethodInvoker(delegate {
UIOperations.CustomizeGrid(radGridView3);
}));
现在我的问题是我不能在课堂内使用this.invoke
。我该如何调用它?我想调用的方法是:
public static void CustomizeGrid(RadGridView Gridview)
{
for (int i = 0; i < Gridview.Columns.Count; i++)
{
Gridview.MasterTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
Gridview.Columns[i].BestFit();
Gridview.Columns[i].HeaderTextAlignment = ContentAlignment.MiddleLeft;
if (Gridview.Columns[i].HeaderText == "Noter")
{
Gridview.Columns["Noter"].MaxWidth = 600;
}
}
// Paint every second row
Gridview.EnableAlternatingRowColor = true;
((GridTableElement)Gridview.TableElement).AlternatingRowColor = Color.Aqua;
// Set row header column width
Gridview.TableElement.RowHeaderColumnWidth = 32;
}
这是我想要调用的地方:
public static void ExcelToGrid(OpenFileDialog openFileDialog1,RadProgressBar radProgressBar1,RadGridView radGridView3)
{
try
{
openFileDialog1.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm";
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK) // Test result.
{
DataTable dt = new DataTable("dataTable");
DataSet dsSource = new DataSet("dataSet");
dt.Reset();
Excel.Workbook ExWorkbook;
Excel.Worksheet ExWorksheet;
Excel.Range ExRange;
Excel.Application ExObj = new Excel.Application();
ExWorkbook = ExObj.Workbooks.Open(openFileDialog1.FileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
ExWorksheet = (Excel.Worksheet)ExWorkbook.Sheets.get_Item(1);
ExRange = ExWorksheet.UsedRange;
for (int Cnum = 1; Cnum <= ExRange.Columns.Count; Cnum++)
{
dt.Columns.Add(new DataColumn((ExRange.Cells[1, Cnum] as Excel.Range).Value2.ToString()));
}
dt.AcceptChanges();
string[] columnNames = new String[dt.Columns.Count];
for (int i = 0; i < dt.Columns.Count; i++)
{
columnNames[0] = dt.Columns[i].ColumnName;
}
for (int Rnum = 2; Rnum <= ExRange.Rows.Count; Rnum++)
{
int MaxValue = ExRange.Rows.Count;
DataRow dr = dt.NewRow();
for (int Cnum = 1; Cnum <= ExRange.Columns.Count; Cnum++)
{
if ((ExRange.Cells[Rnum, Cnum] as Excel.Range).Value2 != null)
{
dr[Cnum - 1] = (ExRange.Cells[Rnum, Cnum] as Excel.Range).Value2.ToString();
}
}
//Write progressbar
radProgressBar1.Maximum = MaxValue;
radProgressBar1.Step = 1;
radProgressBar1.Value1 = Rnum;
radProgressBar1.Text = "Åbner " + Rnum + "/" + MaxValue;
dt.Rows.Add(dr);
dt.AcceptChanges();
}
//Reset progressbar
radProgressBar1.Value1 = 0;
radProgressBar1.Text = "";
//Close workbook
ExWorkbook.Close(true, Missing.Value, Missing.Value);
ExObj.Quit();
if (radGridView3.InvokeRequired)
{
radGridView3.BeginInvoke(new MethodInvoker(delegate { radGridView3.DataSource = dt; }));
}
//Disallow sorting
for (int i = 0; i < radGridView3.Columns.Count; i++)
{
radGridView3.Columns[i].AllowSort = false;
}
}
this.Invoke(new MethodInvoker(delegate
{
UIOperations.CustomizeGrid(radGridView3);
}));
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
答案 0 :(得分:0)
如果您希望将当前设计保留在使用静态帮助程序类的位置,则最好使用所需的逻辑来确定是否需要在该静态方法中调用Invoke
,以便方法的使用者不要必须处理这个问题。 RadGridView是子类Control的子类,因此这些方便的属性可以在RadGridView的实例上使用。
public static void CustomizeGrid(RadGridView Gridview)
{
// check if Invoke is needed
if (Gridview.InvokeRequired)
{
// Yes, so invoke on the Control instance that was supplied
Gridview.Invoke(new MethodInvoker(delegate {
// makes the same call but now you''ll be on the UI thread
CustomizeGrid(Gridview);
}));
return;
}
// all your other logic here
}
静态UIOperations
类中该方法的使用者现在可以简单地调用
UIOperations.CustomizeGrid(radGridView3);
无需担心是否从UI线程调用。