简体   繁体   中英

Datagridview dividing into two parts

I have two datagridview in which i add data manually. First is for assets and second for liability. Is it possible to add this into a single datagridview in a vertical format. I can only arrange them in one after another. i need a border between them in the single datagridview . First of all is it possible? My code is

`

`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##";
        }
    }`

I used a column as barrier between by setting its width to minimum and it's background color to back. The code is as follows

 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--;
            }
        }
    }

I just manually add the columns which i needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM