none
GridView SubItem RRS feed

  • Question

  • Hi everyone. I'm trying to use a grid view in WinForms that when i want to add a discount to an item, the discount should be listed below that item. This is kinda complicated for me, i know we can add another column to have the discount in there but now someone wants me to have the discount below the item. 

    Any help would be appreciated.

    Regards

    Saturday, November 25, 2017 5:40 PM

All replies

  • Hello,

     As you populate the GridView rows, have the code check for item as discount then

    add the new row with discount data.  If item is not as discount then add the normal

    data to the row and continue populating the grid.  To implement this will depend

    greatly on the way you store information about each item.

     

     Example

    class Item
    {
        // Base variables
        //
        public int Quanity { get; set; }
        public string Description { get; set; }
        public decimal Price { get; set; }
        // easy to calc percentage
        // value of zero = no disount
        public decimal Discount { get; set; }
    }

     When your code inserts new row data, the value of Discount determines if

    an extra row is used to display the Discount variable. 

     

     How you implement your database and the structure of item data will help

    to give a more precise solution. The above is a basic frame work.

     Hope this helps :)

    • Proposed as answer by Fei HuModerator Wednesday, November 29, 2017 10:17 AM
    Saturday, November 25, 2017 8:45 PM
  • Hello Dikong42,

    Try the below example, which converts the datasourece to datatable and insert a new row at a position.

      private void button1_Click(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[] { new DataColumn("Quantiy"), new DataColumn("Description"), new DataColumn("Price") });
                dt.Rows.Add("10", "FirstItem", "2");
                dt.Rows.Add("20", "SecondItem", "3");
                dt.Rows.Add("4", "ThirdItem", "4");
    
                dataGridView1.DataSource = dt;
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                DataTable dt = (DataTable)dataGridView1.DataSource;
    
                DataRow row = dt.NewRow();
                row[0] = "";
                row[1] = "-10% discount";
                row[2] = "";
    
                dt.Rows.InsertAt(row,1);
    
                //dataGridView1.Rows.Insert(0, " ", "-10% discount");
            }

    And if you want to select the index of certain row , you could use DataTable.Rows.Indexof method, which will return a position of int type.

    https://msdn.microsoft.com/en-us/library/system.data.datarowcollection.indexof(v=vs.110).aspx

    Sincerely,

    Fei Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Bob Ding Monday, November 27, 2017 8:11 AM
    Sunday, November 26, 2017 8:18 AM
    Moderator