Datagirdview和TabControl的问题 [英] Datagirdview and tabcontrol issue
问题描述
我遇到一个奇怪的行为在我的WinForms .NET 4的应用程序。我有有两个的TabPages一个TabControl形式,用户在选择tabpage1数据和点击GO按钮,有这势必用户选择(数据表)的结果DataGridView控件。之后我把datagridview的数据源的,我加入一个排在我网格的数据源的顶部(0指数),那么我在该行申请某些格式(datagirdview.rows [0])。
我可以看到应用到调试器行我的格式,但只要标签选择代码运行时,我行格式化(的isFrozen,背景色等)已经一去不复返了。
当我选择TabPage的第一和网格和格式化后的绑定设置数据源,它工作正常。
只是新添加的行会失去格式化,我有一个类似的应用中,我加入了一排这样的,但它工作正常,在当前的应用程序,我使用的是BackgroundWorker的运行从RunWorkerCompleted这个代码,而在以前的应用程序,我不使用的BackgroundWorker。
下面是代码
无效bw_RunWorkerCompleted(对象发件人,RunWorkerCompletedEventArgs E)
{
如果(e.Cancelled&安培;!&安培; e.Error == NULL)
{
如果(((数据表)e.Result).Rows.Count> 0)
{
//tabControl1.SelectTab(tabPage2);如果我从这里叫那么排格式保留
grdDistProcessing.DataSource =((数据表)e.Result);
formatGrid();
loadStoresGrid();
AddTotalsRowInEnd();
SetTotalsOfTotalRow();
tabControl1.SelectTab(tabPage2);
}
}
this.tsStatus.Text =的String.Empty;
}
下面是AddTotalsRowInEnd方式:
私人无效AddTotalsRowInEnd()
{
字体F =新System.Drawing.Font(宋体,8,FontStyle.Bold);
的DataRow博士=((数据表)grdDistProcessing.DataSource).NewRow();
dr.ItemArray =((数据表)grdDistProcessing.DataSource).Rows [0] .ItemArray;
博士[Itemlookupcode] =大汇总;
博士[大小] =;
博士[COLORS] =;
博士[说明] =;
((数据表)grdDistProcessing.DataSource).Rows.InsertAt(DR,0);
grdDistProcessing.Rows [0] .Frozen = TRUE;
grdDistProcessing.Rows [0] = .DefaultCellStyle.BackColor Color.BurlyWood;
grdDistProcessing.Rows [0] = .DefaultCellStyle.ForeColor Color.Black;
grdDistProcessing.Rows [0] .DefaultCellStyle.Font = F;
grdDistProcessing.Rows [0] .ReadOnly = TRUE;
grdDistProcessing.Refresh();
}
和这里是我的DoWork的:
无效bw_DoWork(对象发件人,DoWorkEventArgs E)
{
试
{
BackgroundWorker的bWorkder =发件人为BackgroundWorker的;
DistVariablesTransfer DTR = e.Argument为DistVariablesTransfer;
bWorkder.ReportProgress(10);
cProcess亲=新cProcess();
e.Result = pro.loadDistribution(dtr.pWarehouseID,dtr.pStores,dtr.pStyle,dtr.pColor,dtr.pSize,dtr.pDateFrom,dtr.pDateTo,dtr.pIncOrdQtyForSrc,dtr.PCheckDestinationTranferOut);
}
赶上(异常前)
{
MessageBox.Show(ex.Message,this.Text,MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
而不是做的:
grdDistProcessing.Rows [0] = .DefaultCellStyle.BackColor Color.BurlyWood;
grdDistProcessing.Rows [0] = .DefaultCellStyle.ForeColor Color.Black;
使用了grdDistProcessing的CellFormatting事件(Display类的),像这样的:
私人无效grdDistProcessing_CellFormatting(对象发件人,DataGridViewCellFormattingEventArgs E)
{
e.CellStyle.BackColor = Color.BurlyWood;
e.CellStyle.ForeColor = Color.Black;
}
应该渲染得更快。
I am experiencing a strange behavior in my winforms .net 4 application. I have a form with a tabcontrol having two tabpages, user selects data on tabpage1 and clicks GO button, there is a dataGridView control which is bound to a results of user selection (a datatable). After I set datasource of datagridview, I am adding a row at top(0 index) of my grid's datasource, then I am applying some formatting on that row (datagirdview.rows[0]).
I can see my formatting applied to the row in debugger, but as soon as tab selection code runs, my row formatting(isFrozen, BackColor etc) is gone.
When I selects the tabpage first and bind set datasource of gird and formatting afterwards, it works fine.
only newly added row is loosing formatting, I have a similar application in which I am adding a row like this, but it is working fine, in current application I am using a backgroundWorker and running this code from RunWorkerCompleted, while in previous application I am not using backGroundWorker.
Below is the code
void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (!e.Cancelled && e.Error == null)
{
if (((DataTable)e.Result).Rows.Count > 0)
{
//tabControl1.SelectTab(tabPage2); if I call from here then row formatting retains
grdDistProcessing.DataSource = ((DataTable)e.Result);
formatGrid();
loadStoresGrid();
AddTotalsRowInEnd();
SetTotalsOfTotalRow();
tabControl1.SelectTab(tabPage2);
}
}
this.tsStatus.Text = string.Empty;
}
Here is AddTotalsRowInEnd Method:
private void AddTotalsRowInEnd()
{
Font f = new System.Drawing.Font("Arial", 8, FontStyle.Bold);
DataRow dr = ((DataTable)grdDistProcessing.DataSource).NewRow();
dr.ItemArray = ((DataTable)grdDistProcessing.DataSource).Rows[0].ItemArray;
dr["Itemlookupcode"] = "Grand Totals";
dr["Size"] = "";
dr["COLORS"] = "";
dr["DESCRIPTIONS"] = "";
((DataTable)grdDistProcessing.DataSource).Rows.InsertAt(dr, 0);
grdDistProcessing.Rows[0].Frozen = true;
grdDistProcessing.Rows[0].DefaultCellStyle.BackColor = Color.BurlyWood;
grdDistProcessing.Rows[0].DefaultCellStyle.ForeColor = Color.Black;
grdDistProcessing.Rows[0].DefaultCellStyle.Font = f;
grdDistProcessing.Rows[0].ReadOnly = true;
grdDistProcessing.Refresh();
}
and Here is my DoWork:
void bw_DoWork(object sender, DoWorkEventArgs e)
{
try
{
BackgroundWorker bWorkder = sender as BackgroundWorker;
DistVariablesTransfer dtr = e.Argument as DistVariablesTransfer;
bWorkder.ReportProgress(10);
cProcess pro = new cProcess();
e.Result = pro.loadDistribution(dtr.pWarehouseID, dtr.pStores, dtr.pStyle, dtr.pColor, dtr.pSize, dtr.pDateFrom, dtr.pDateTo, dtr.pIncOrdQtyForSrc, dtr.PCheckDestinationTranferOut);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Instead of doing:
grdDistProcessing.Rows[0].DefaultCellStyle.BackColor = Color.BurlyWood;
grdDistProcessing.Rows[0].DefaultCellStyle.ForeColor = Color.Black;
Use the CellFormatting event (Display category) for grdDistProcessing, like this:
private void grdDistProcessing_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
e.CellStyle.BackColor = Color.BurlyWood;
e.CellStyle.ForeColor = Color.Black;
}
It should render faster too.
这篇关于Datagirdview和TabControl的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!