Datagridview分为两部分

时间:2017-12-20 06:39:39

标签: c#

我有两个datagridview我手动添加数据。第一是资产,第二是责任。是否可以以垂直格式将其添加到单个datagridview中。 我只能一个接一个地安排他们。我需要在单datagridview之间使用它们之间的边框。 首先是可能吗? 我的代码是

`

`private void btnshow_Click(object sender,EventArgs e)

    {
        int a = 0, b = 0;
        decimal opstk = 0, clstk = 0,val=0;
        string dateas  = dtpdate.Value.ToString("yyyy-MM-dd");
        dataGridView1.Rows.Clear();
        dataGridView2.Rows.Clear();

        string qry = "select x.head_id, head_name,coalesce(op,0) as op from (select master_id, head_id, head_name, current_balance from heads where master_id='A' and not is_group order by head_id) as x left join (select head_id, sum(debit_amount-credit_amount) as op from ledger_detail ld, ledger l where ld.ledger_number=l.ledger_number and ledger_date<='"+dateas+"' group by head_id) as y on x.head_id=y.head_id order by head_id";
        DataTable dt = obj.table(qry);
        a = dt.Rows.Count;
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            dataGridView1.Rows.Add();
            dataGridView1.Rows[i].Cells[0].Value = dt.Rows[i]["head_id"];
            dataGridView1.Rows[i].Cells[1].Value = dt.Rows[i]["head_name"];
            dataGridView1.Rows[i].Cells[2].Value = dt.Rows[i]["op"];


        }
        string qry2 = "select 'Total' as head_name,sum(op) as op from(select x.head_id, head_name,coalesce(op,0) as op from(select head_id, head_name,current_balance from heads where master_id='A' and not is_group order by head_id ) as x left join (select head_id, sum(debit_amount-credit_amount) as op from ledger_detail ld, ledger l where ld.ledger_number=l.ledger_number and ledger_date<='"+dateas+"'  group by head_id) as y on x.head_id=y.head_id) as z";
        //string qry2 = "select 'Total' as head_name, sum(op) as op from(select x.head_id, head_name,coalesce(op,0) as op from(select head_id,head_name from heads where master_id='A'and not is_group order by head_id ) as x left join (select head_id, sum(debit_amount-credit_amount) as op from ledger_detail ld, ledger l where ld.ledger_number= l.ledger_number  and ledger_date<='" + dateas + "' group by head_id) as y on x.head_id=y.head_id) as z";
        DataTable dt2 = obj.table(qry2);
        dataGridView1.Rows.Add();
        dataGridView1.Rows[a].Cells[0].Value = dt2.Rows[0]["head_name"];
        dataGridView1.Rows[a].Cells[3].Value = dt2.Rows[0]["op"];
        ////////////////////////////////////////////////////////////////////////////////
        string qry1 = "select x.head_id, head_name, coalesce(op,0) as op from (select master_id, head_id, head_name, current_balance from heads where master_id='B' and not is_group order by head_id) as x left join (select head_id, sum(debit_amount-credit_amount) as op from ledger_detail ld, ledger l where ld.ledger_number=l.ledger_number and ledger_date<='" + dateas + "' group by head_id) as y on x.head_id=y.head_id order by head_id";
        DataTable dt1 = obj.table(qry1);
        b = dt1.Rows.Count;
        for (int i = 0; i < dt1.Rows.Count; i++)
        {
            dataGridView2.Rows.Add();
            dataGridView2.Rows[i].Cells[0].Value = dt1.Rows[i]["head_id"];
            dataGridView2.Rows[i].Cells[1].Value = dt1.Rows[i]["head_name"];
            dataGridView2.Rows[i].Cells[2].Value = Convert.ToDecimal(dt1.Rows[i]["op"]) * -1;
        }
        string qry3 = "select 'Total' as head_name, sum(op) as op from(select x.head_id, head_name,coalesce(op,0) as op from(select head_id, head_name,current_balance from heads where master_id='B' and not is_group order by head_id ) as x left join (select head_id, sum(debit_amount-credit_amount) as op from ledger_detail ld, ledger l where ld.ledger_number=l.ledger_number and ledger_date<='" + dateas + "'  group by head_id) as y on x.head_id=y.head_id) as z";
        DataTable dt3 = obj.table(qry3);
        dataGridView2.Rows.Add();
        dataGridView2.Rows[b].Cells[0].Value = dt3.Rows[0]["head_name"];
        dataGridView2.Rows[b].Cells[3].Value = Convert.ToDecimal(dt3.Rows[0]["op"]) * -1;

        string qry4 = "select 'Closing Stock' as head_name, sum(stock) as op from (select sum(costbase*current_stock) as stock from itemstock ik,items i where i.item_id=ik.item_id  union select sum(debit-credit) as stock from view_total_trans where transdate >='"+dateas+"' ) as x";
        DataTable dt4 = obj.table(qry4);
        dataGridView1.Rows.Add();
        dataGridView1.Rows[a + 1].Cells[0].Value = dt4.Rows[0]["head_name"];
        dataGridView1.Rows[a + 1].Cells[3].Value = dt4.Rows[0]["op"];

        opstk =Convert.ToDecimal(dataGridView1.Rows[a].Cells[3].Value) + Convert.ToDecimal(dataGridView1.Rows[a+1].Cells[3].Value);
        clstk = Convert.ToDecimal(dataGridView2.Rows[b].Cells[3].Value);
        val = opstk - clstk;
        if(val>0)
        {
            dataGridView2.Rows.Add();
            dataGridView2.Rows[b + 1].Cells[0].Value = "Net Loss";
            dataGridView2.Rows[b + 1].Cells[3].Value = val;
            dataGridView2.Rows[b + 1].DefaultCellStyle.Format = "0.00##";

            dataGridView2.Rows.Add();
            dataGridView2.Rows[b + 2].Cells[3].Value = clstk + val;
            dataGridView2.Rows[b + 2].DefaultCellStyle.Font = new Font("Arial", 9, FontStyle.Bold);
            dataGridView2.Rows[b + 2].DefaultCellStyle.Format = "0.00##";
            dataGridView1.Rows.Add();
            dataGridView1.Rows[a + 2].Cells[3].Value = opstk;
            dataGridView1.Rows[a + 2].DefaultCellStyle.Font = new Font("Arial", 9, FontStyle.Bold);
            dataGridView1.Rows[a + 2].DefaultCellStyle.Format = "0.00##";
        }
        else
        {
            dataGridView1.Rows.Add();
            dataGridView1.Rows[a + 2].Cells[0].Value = "Net Profit";
            dataGridView1.Rows[a + 2].Cells[3].Value = val * -1;
            dataGridView1.Rows[a + 2].DefaultCellStyle.Format = "0.00##";



            dataGridView2.Rows.Add();
            dataGridView2.Rows[b + 1].Cells[3].Value = clstk;
            dataGridView2.Rows[b + 1].DefaultCellStyle.Font = new Font("Arial", 9, FontStyle.Bold);
            dataGridView2.Rows[b + 1].DefaultCellStyle.Format = "0.00##";
            dataGridView1.Rows.Add();
            dataGridView1.Rows[a + 3].Cells[3].Value = opstk+Convert.ToDecimal(dataGridView1.Rows[a+2].Cells[3].Value);
            dataGridView1.Rows[a + 3].DefaultCellStyle.Font = new Font("Arial", 9, FontStyle.Bold);
            dataGridView1.Rows[a + 3].DefaultCellStyle.Format = "0.00##";
        }
    }`

1 个答案:

答案 0 :(得分:0)

我通过将其宽度设置为最小值并使用背景颜色来使用列作为屏障。 代码如下

 private void btnshow_Click(object sender, EventArgs e)
    {
        int a = 0, b = 0;
        decimal opstk = 0, clstk = 0,val=0;
        string dateas  = dtpdate.Value.ToString("yyyy-MM-dd");
        dataGridView1.Rows.Clear();
        string qry = "select x.head_id, head_name,coalesce(op,0) as op from (select master_id, head_id, head_name, current_balance from heads where master_id='A' and not is_group order by head_id) as x left join (select head_id, sum(debit_amount-credit_amount) as op from ledger_detail ld, ledger l where ld.ledger_number=l.ledger_number and ledger_date<='" + dateas + "' group by head_id) as y on x.head_id=y.head_id order by head_id";
        DataTable dt = obj.table(qry);
        a = dt.Rows.Count;
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            dataGridView1.Rows.Add();
            dataGridView1.Rows[i].Cells[0].Value = dt.Rows[i]["head_id"];
            dataGridView1.Rows[i].Cells[1].Value = dt.Rows[i]["head_name"];
            dataGridView1.Rows[i].Cells[2].Value = dt.Rows[i]["op"];
        }
        ////////////////////////////////////////////////////////////////////////////////
        string qry1 = "select x.head_id, head_name, coalesce(op,0) as op from (select master_id, head_id, head_name, current_balance from heads where master_id='B' and not is_group order by head_id) as x left join (select head_id, sum(debit_amount-credit_amount) as op from ledger_detail ld, ledger l where ld.ledger_number=l.ledger_number and ledger_date<='" + dateas + "' group by head_id) as y on x.head_id=y.head_id order by head_id";
        DataTable dt1 = obj.table(qry1);
        b = dt1.Rows.Count;
        for (int i = 0; i < dt1.Rows.Count; i++)
        {
            dataGridView1.Rows.Add();//
            dataGridView1.Rows[i].Cells[5].Value = dt1.Rows[i]["head_id"];//
            dataGridView1.Rows[i].Cells[6].Value = dt1.Rows[i]["head_name"];//
            dataGridView1.Rows[i].Cells[7].Value = Convert.ToDecimal(dt1.Rows[i]["op"]) * -1;//
        }
        ///////////////////////////
        if (a > b)
        {
            string qry2 = "select 'Total' as head_name,sum(op) as op from(select x.head_id, head_name,coalesce(op,0) as op from(select head_id, head_name,current_balance from heads where master_id='A' and not is_group order by head_id ) as x left join (select head_id, sum(debit_amount-credit_amount) as op from ledger_detail ld, ledger l where ld.ledger_number=l.ledger_number and ledger_date<='" + dateas + "'  group by head_id) as y on x.head_id=y.head_id) as z";
            DataTable dt2 = obj.table(qry2);
            dataGridView1.Rows.Add();
            dataGridView1.Rows[a].Cells[0].Value = dt2.Rows[0]["head_name"];
            dataGridView1.Rows[a].Cells[3].Value = dt2.Rows[0]["op"];
            string qry3 = "select 'Total' as head_name, sum(op) as op from(select x.head_id, head_name,coalesce(op,0) as op from(select head_id, head_name,current_balance from heads where master_id='B' and not is_group order by head_id ) as x left join (select head_id, sum(debit_amount-credit_amount) as op from ledger_detail ld, ledger l where ld.ledger_number=l.ledger_number and ledger_date<='" + dateas + "'  group by head_id) as y on x.head_id=y.head_id) as z";
            DataTable dt3 = obj.table(qry3);
            dataGridView1.Rows.Add();//
            dataGridView1.Rows[a].Cells[5].Value = dt3.Rows[0]["head_name"];//
            dataGridView1.Rows[a].Cells[8].Value = Convert.ToDecimal(dt3.Rows[0]["op"]) * -1;//
            string qry4 = "select 'Closing Stock' as head_name, sum(stock) as op from (select sum(costbase*current_stock) as stock from itemstock ik,items i where i.item_id=ik.item_id  union select sum(debit-credit) as stock from view_total_trans where transdate >='" + dateas + "' ) as x";
            DataTable dt4 = obj.table(qry4);
            dataGridView1.Rows.Add();
            dataGridView1.Rows[a + 1].Cells[0].Value = dt4.Rows[0]["head_name"];
            dataGridView1.Rows[a + 1].Cells[3].Value = dt4.Rows[0]["op"];
            opstk = Convert.ToDecimal(dataGridView1.Rows[a].Cells[3].Value) + Convert.ToDecimal(dataGridView1.Rows[a + 1].Cells[3].Value);
            MessageBox.Show("opstk " + opstk);
            clstk = Convert.ToDecimal(dataGridView1.Rows[a].Cells[8].Value);
            MessageBox.Show("clstk " + clstk);
            val = opstk - clstk;
            //////////////////***********************************************************************************////////////////////////
            if (val > 0)
            {
                dataGridView1.Rows.Add();//
                dataGridView1.Rows[a + 2].Cells[5].Value = "Net Loss";//
                dataGridView1.Rows[a + 2].Cells[8].Value = val;// 
                dataGridView1.Rows.Add();//
                dataGridView1.Rows[a + 3].Cells[8].Value = clstk + val;//
                dataGridView1.Rows[a + 3].DefaultCellStyle.Font = new Font("Arial", 9, FontStyle.Bold);//
                dataGridView1.Rows.Add();
                dataGridView1.Rows[a + 3].Cells[3].Value = opstk;
                dataGridView1.Rows[a + 3].DefaultCellStyle.Font = new Font("Arial", 9, FontStyle.Bold);
            }
            else
            {
                dataGridView1.Rows.Add();
                dataGridView1.Rows[a + 2].Cells[0].Value = "Net Profit";
                dataGridView1.Rows[a + 2].Cells[3].Value = val * -1;
                dataGridView1.Rows.Add();//
                dataGridView1.Rows[a + 3].Cells[8].Value = clstk;//
                dataGridView1.Rows[a + 3].DefaultCellStyle.Font = new Font("Arial", 9, FontStyle.Bold);
                dataGridView1.Rows.Add();
                dataGridView1.Rows[a + 3].Cells[3].Value = opstk + Convert.ToDecimal(dataGridView1.Rows[a + 2].Cells[3].Value);
                dataGridView1.Rows[a + 3].DefaultCellStyle.Font = new Font("Arial", 9, FontStyle.Bold);

            }
        }

        else
        {
            string qry2 = "select 'Total' as head_name,sum(op) as op from(select x.head_id, head_name,coalesce(op,0) as op from(select head_id, head_name,current_balance from heads where master_id='A' and not is_group order by head_id ) as x left join (select head_id, sum(debit_amount-credit_amount) as op from ledger_detail ld, ledger l where ld.ledger_number=l.ledger_number and ledger_date<='" + dateas + "'  group by head_id) as y on x.head_id=y.head_id) as z";
            DataTable dt2 = obj.table(qry2);
            dataGridView1.Rows.Add();
            dataGridView1.Rows[b].Cells[0].Value = dt2.Rows[0]["head_name"];
            dataGridView1.Rows[b].Cells[3].Value = dt2.Rows[0]["op"];
            string qry3 = "select 'Total' as head_name, sum(op) as op from(select x.head_id, head_name,coalesce(op,0) as op from(select head_id, head_name,current_balance from heads where master_id='B' and not is_group order by head_id ) as x left join (select head_id, sum(debit_amount-credit_amount) as op from ledger_detail ld, ledger l where ld.ledger_number=l.ledger_number and ledger_date<='" + dateas + "'  group by head_id) as y on x.head_id=y.head_id) as z";
            DataTable dt3 = obj.table(qry3);
            dataGridView1.Rows.Add();//
            dataGridView1.Rows[b].Cells[5].Value = dt3.Rows[0]["head_name"];
            dataGridView1.Rows[b].Cells[8].Value = Convert.ToDecimal(dt3.Rows[0]["op"]) * -1;//
            string qry4 = "select 'Closing Stock' as head_name, sum(stock) as op from (select sum(costbase*current_stock) as stock from itemstock ik,items i where i.item_id=ik.item_id  union select sum(debit-credit) as stock from view_total_trans where transdate >='" + dateas + "' ) as x";
            DataTable dt4 = obj.table(qry4);
            dataGridView1.Rows.Add();
            dataGridView1.Rows[b + 1].Cells[0].Value = dt4.Rows[0]["head_name"];
            dataGridView1.Rows[b + 1].Cells[3].Value = dt4.Rows[0]["op"];
            opstk = Convert.ToDecimal(dataGridView1.Rows[b].Cells[3].Value) + Convert.ToDecimal(dataGridView1.Rows[b + 1].Cells[3].Value);
            clstk = Convert.ToDecimal(dataGridView1.Rows[b].Cells[8].Value);
            val = opstk - clstk;
            //////////////////***********************************************************************************////////////////////////
            if (val > 0)
            {
                dataGridView1.Rows.Add();//
                dataGridView1.Rows[b + 2].Cells[5].Value = "Net Loss";//
                dataGridView1.Rows[b + 2].Cells[8].Value = val;//
                dataGridView1.Rows.Add();//
                dataGridView1.Rows[b + 3].Cells[8].Value = clstk + val;//
                dataGridView1.Rows[b + 3].DefaultCellStyle.Font = new Font("Arial", 9, FontStyle.Bold);//
                dataGridView1.Rows.Add();
                dataGridView1.Rows[b + 3].Cells[3].Value = opstk;
                dataGridView1.Rows[b + 3].DefaultCellStyle.Font = new Font("Arial", 9, FontStyle.Bold);
            }
            else
            {
                dataGridView1.Rows.Add();
                dataGridView1.Rows[b + 2].Cells[0].Value = "Net Profit";
                dataGridView1.Rows[b + 2].Cells[3].Value = val * -1;
                dataGridView1.Rows.Add();//
                dataGridView1.Rows[b + 3].Cells[8].Value = clstk;//
                dataGridView1.Rows[b + 3].DefaultCellStyle.Font = new Font("Arial", 9, FontStyle.Bold);
                dataGridView1.Rows.Add();
                dataGridView1.Rows[b + 3].Cells[3].Value = opstk + Convert.ToDecimal(dataGridView1.Rows[a + 2].Cells[3].Value);
                dataGridView1.Rows[b + 3].DefaultCellStyle.Font = new Font("Arial", 9, FontStyle.Bold);
            }
        }

        for (int i = 0; i < dataGridView1.Rows.Count; i++)
        {
            if (dataGridView1.Rows[i].Cells[0].Value == null && dataGridView1.Rows[i].Cells[2].Value == null && dataGridView1.Rows[i].Cells[3].Value == null)
            {
                dataGridView1.Rows.RemoveAt(i);
                i--;
            }
        }
    }

我只需手动添加我需要的列。