未绑定的datagridview未显示首次使用时添加的行

时间:2016-08-04 08:51:21

标签: c# datagridview

这是代码的本质

private System.Windows.Forms.DataGridViewTextBoxColumn fi_sym;
private System.Windows.Forms.DataGridViewTextBoxColumn qty;
private System.Windows.Forms.DataGridViewTextBoxColumn Cost4Item;
private System.Windows.Forms.DataGridViewTextBoxColumn cost;
private System.Windows.Forms.DataGridViewTextBoxColumn price;
private System.Windows.Forms.DataGridViewTextBoxColumn Profit;
private System.Windows.Forms.DataGridViewTextBoxColumn PnLpc;
private System.Windows.Forms.DataGridViewTextBoxColumn Value;
private System.Windows.Forms.DataGridViewTextBoxColumn New_Cost;
private System.Windows.Forms.DataGridViewTextBoxColumn New_Mkt_Value;
private System.Windows.Forms.DataGridViewTextBoxColumn Cost_Chg_Pc;
private System.Windows.Forms.DataGridViewTextBoxColumn Profit_Change_pc;

this.fi_sym = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.qty = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Cost4Item = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.cost = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.price = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Profit = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.PnLpc = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Value = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.New_Cost = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.New_Mkt_Value = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Cost_Chg_Pc = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Profit_Change_pc = new System.Windows.Forms.DataGridViewTextBoxColumn();

System.Windows.Forms.DataGridView chgDgv = new System.Windows.Forms.DataGridView();
this.chgDgv.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
        this.fi_sym,
        this.qty,
        this.Cost4Item,
        this.cost,
        this.price,
        this.Profit,
        this.PnLpc,
        this.Value,
        this.New_Cost,
        this.New_Mkt_Value,
        this.Cost_Chg_Pc,
        this.Profit_Change_pc});

public void setup(string[] fs_sym, decimal[] qty, decimal[] cost, decimal[] price)
{
setMsg("");
int len = Math.Min(Math.Min(Math.Min(fs_sym.Length, qty.Length), cost.Length), price.Length);
if (len != Math.Max(Math.Max(Math.Max(fs_sym.Length, qty.Length), cost.Length), price.Length)) setMsg("Caution: not all input have the same Length; show the least=" + len);
for (int i = 0; i < len-1; i++)
{
    decimal costPerShr = cost[i] / qty[i], value = qty[i] * price[i], profit = value - cost[i], PnLpc = profit * 100 / cost[i], newCost = cost[i], newMktValu = value, costChgPc = 0, profitChgPc = 0;
    chgDgv.Rows.Add(new object[] { fs_sym[i], qty[i], cost[i], costPerShr, price[i], profit, PnLpc, value, newCost, newMktValu, costChgPc, profitChgPc });
    chgDgv.Rows[i].Visible = true;
}

addMsg("after adding object rows, total rows=" + chgDgv.Rows.Count);

// the following is immaterial as to the disappearance of the added rows on exit
int[] widths = { 0, 60, 65, 80,     40, 65, 40, 80,  65,   80,   70,   70};
//              q, cstb cst/shr lq prf Pnl% val nuCst nuMkt cstΔ% prfΔ%
string[] fmt = { "", "n2", "c2","c2","c2","c2", "c2", "c2", "c2", "c2", "c2", "c2" };
Util.format_numeric_Colmn(chgDgv, widths, fmt, this.Text);
addMsg("after format, total rows=" + chgDgv.Rows.Count);

}

在我的测试中,我设置了每个76个项目的4个数组来调用设置。在离开设置之前有77行但是除了空行之外它们都消失了

我确实将添加的行显示出来,但删除了原始列并替换为DataTable列。然后我遇到了无法使用格式字符串格式化列的问题,如n2,c2。 为了节省空间,这里是缩写代码

    DataTable dt = new DataTable();
    public void setup(string[] fs_sym, ...)
    {
          ...
        if (dt.Columns.Count<1){
            for (int i = 0; i < 12; i++) dt.Columns.Add(new DataColumn(chgDgv.Columns[i].Name));
            chgDgv.Columns.Clear();
    }

    int len ...
        ...
        for (int i = 0; i < len-1; i++)
        {
            ...dt.Rows.Add(new object[] { fs_sym[i], qty[i],.. });
        }
        chgDgv.DataSource = dt;
        int[] widths = { 0, 60, 65, 80,     ...

        string[] fmt = { "", "n2", "c2","c2",...};
        Util.format_numeric_Colmn(chgDgv, widths, fmt, this.Text);
    }
//Util.format_numeric_Column has been tested previously and works for other datagridviews with bounded datatable in dataset
/// <summary>
/// format the DataGridView dgv's data columns with widths, alignment and string format cellFormat
/// </summary>
/// <param name="dgv"></param>
/// <param name="widths">should have the same number of the columns in the dgv, default is used for missing ones at end<br>
/// +ve right aligned, -ve left aligned, -1 invisible, 0 default and right aligned</br></param>
/// <param name="cellFormat">should have the same number of the columns in the dgv but excess ignored, default is used for missing ones at end<br>
/// xn or d or nothing</br><br>
/// <indent>where x can be C for currency, or N for numeric, and n is the number decimal palces after the decimal<br>
/// d is for date</br></indent></br></param>
public static void format_numeric_Colmn(DataGridView dgv, int[] widths, string[] cellFormat, string msgCaption)
{
    if (dgv.Rows.Count > 1)
        for (int i = 0; i < dgv.Columns.Count; i++)
            format_numeric_Colmn(dgv.Columns[i], i < widths.Length ? widths[i] : 0, i < cellFormat.Length ? cellFormat[i] : "");
    else MessageBox.Show("Please ensure the DataGridView.DataSource has been set properly\r\n" +
        dgv.Name + ".DataSource=" + dgv.DataSource.ToString(), msgCaption + " - format_numeric_Colmn",
            MessageBoxButtons.OK, MessageBoxIcon.Stop);
}

我还能做什么?

0 个答案:

没有答案