dcsimg
Results 1 to 10 of 10

Thread: How to combine 2 data grid view to one data grid view and insert it to new table

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    83

    How to combine 2 data grid view to one data grid view and insert it to new table

    Hi All,

    I want to improve my program (MONITORING OUTPUT SYSTEM) and create report for it.

    Here it looks like:
    datagridview on the left is from datasource it auto count the OUTPUT, then datagridview on the right is manual data means no datasource and users must keyin the Target number they want.
    Name:  4.jpg
Views: 70
Size:  16.3 KB

    after they key in target they will click Refresh and it will count the Balance by OUTPUT - TARGET.
    Name:  5.jpg
Views: 70
Size:  18.6 KB

    Now I want after user click Refresh button , I want to get all the data from 2 data grid view and combine to new data grid view below and I want to insert it to new table in my sql .
    How can I move the data from 2 datagridview to 1 datagridview ?
    I am thinking if it will work this way:
    datagridview_Report = datagridview_OUTPUT.datasource ,datagridview_TARGET.datasource.
    really dont know what to do.

    Please help me and thank you in advanced.
    Name:  6.jpg
Views: 65
Size:  20.3 KB

    this is related to my question before:
    http://www.vbforums.com/showthread.p...alculate-value
    Last edited by BONITO; Jul 2nd, 2019 at 10:35 PM. Reason: Image not showing

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,589

    Re: How to combine 2 data grid view to one data grid view and insert it to new table

    What's the relationship between the data in the two grids? Is it 1:1 based on row index, i.e. the first row of each grid effectively constitutes a single record, etc? Is it parent/child, i.e. each row in the first grid corresponds to zero, one or more rows in the second grid? Something else? Please provide a FULL and CLEAR explanation of the problem.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    83

    Re: How to combine 2 data grid view to one data grid view and insert it to new table

    Hi Sir,
    Here is my methods to calculate the Balance column. I got data from first dgv Output column and Minus it to 2nd dgv Target column.
    that's the only relationship they have I think it is called 1:1 based on row index.

    Now my problem is I want to combine it to 1 dgv all 5 columns. is that possible ?
    Please help me. if not what can you suggest ?

    Code:
      void calculateTarget_H5()
            {
                try
                {
                    if (dataGridView_H5.Rows.Count > 0)
                    {
    
                        for (int i = 0; i < dataGridView_H5.Rows.Count; i++)
                        {
                            if (dataGridView_H5_1.Rows[i].Cells[0].Value != null)
                            {
                                int sum = Convert.ToInt32(dataGridView_H5_1.Rows[i].Cells[0].Value.ToString()) - Convert.ToInt32(dataGridView_H5.Rows[i].Cells[2].Value.ToString());
                                dataGridView_H5_1.Rows[i].Cells[1].Value = sum.ToString();
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("Please input all Target", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,589

    Re: How to combine 2 data grid view to one data grid view and insert it to new table

    Is there a specific reason that you don't want to just use one grid in the first place?

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    83

    Re: How to combine 2 data grid view to one data grid view and insert it to new table

    hi Sir,

    I try to used 1 grid view but I have problem when I click Load button again it refresh all data in data grid. example :
    I click Load button , It query the count of Output from data source with 3 columns only.
    Name:  7.jpg
Views: 19
Size:  22.8 KB

    I click checkbox show target column, it added 2 columns in grid. TARGET & BALANCE. User must key in manually the Target .
    Name:  8.jpg
Views: 20
Size:  22.6 KB

    after key in Target User will click the Load target button to calculate the BALANCE = OUTPUT-TARGET.
    Name:  9.jpg
Views: 22
Size:  22.1 KB

    Now the problem is , I need to click the Load button every 5 minutes to count the output for real time.
    when I do that it delete again the data in Target column that the user entered.

    Name:  10.jpg
Views: 20
Size:  22.8 KB
    is there a way if I click Load button it will only refresh the 3 original columns I selected in my query ?

    here is my method to load the 3 columns from datasource.
    Code:
     void H8()
            {
                try
                {
                    conn.Open();
                    cmd = new OleDbCommand("SELECT LINE_NO,STATION,COUNT (DISTINCT(MO_SN))AS INPUT from dqc342 " +
                                   "where TEST_DTTM between to_date('" + dtpFrom.Value.ToString("yyyyMMdd 08:00:00") + "','yyyymmdd hh24:mi:ss')and to_date('" + dtpTo.Value.ToString("yyyyMMdd 07:59:59") + "','yyyymmdd hh24:mi:ss')" +
                                   "and station in('RC4','VMI2')" +
                                   "and LINE_NO='H8'GROUP BY LINE_NO,MODEL,SN_TYPE,STATION,PROD_TYPE ORDER BY STATION", conn);
                    reader = cmd.ExecuteReader();
                    if (reader.HasRows)
                    {
    
                        dt = new DataTable();
                        dt.Load(reader);
                        dataGridView_H8.DataSource = dt;
                        conn.Close();
                        reader.Close();
                        UpdateFont_H8();
                       
                    }
                   
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    conn.Close();
    
                }
    
            }

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    83

    Re: How to combine 2 data grid view to one data grid view and insert it to new table

    Hi Sir,
    Below is the reason why I can't used 1 grid, because I need to refresh every time the 3 original columns from data table and it will affect the new 2 columns I added to key in Target and Calculate Balance.

    kindly help me if there is other way to show what I want.
    thank you in advanced.

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,589

    Re: How to combine 2 data grid view to one data grid view and insert it to new table

    Quote Originally Posted by BONITO View Post
    Below is the reason why I can't used 1 grid, because I need to refresh every time the 3 original columns from data table and it will affect the new 2 columns I added to key in Target and Calculate Balance.
    I don't really see how that's a reason. If the data currently in the second grid is relative to the data in the first then surely you would want the data in the second grid to be affected if you change the data in the first, so the fact that it is if you use one grid seems like a good thing.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    83

    Re: How to combine 2 data grid view to one data grid view and insert it to new table

    Quote Originally Posted by jmcilhinney View Post
    I don't really see how that's a reason. If the data currently in the second grid is relative to the data in the first then surely you would want the data in the second grid to be affected if you change the data in the first, so the fact that it is if you use one grid seems like a good thing.
    But is there a way not to affect the Target column only if i use 1 grid only ? so that user will not key in always the Target ? I want if load again it will affect the 3 original columns only and the column Balance. but the target must be fix.
    if I make 2 grid i can do it. but if only one grid cannot. because it will refresh all the grid.
    Last edited by BONITO; Jul 3rd, 2019 at 10:51 PM.

  9. #9
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,589

    Re: How to combine 2 data grid view to one data grid view and insert it to new table

    Quote Originally Posted by BONITO View Post
    But is there a way not to affect the Target column only if i use 1 grid only ? so that user will not key in always the Target ? I want if load again it will affect the 3 original columns only and the column Balance. but the target must be fix.
    if I make 2 grid i can do it. but if only one grid cannot. because it will refresh all the grid.
    If you populate a DataTable at the start and bind that, then update the existing rows in that DataTable rather than removing the existing rows and adding new ones or creating a whole new DataTable then yes, the other data will remain unaffected.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    83

    Re: How to combine 2 data grid view to one data grid view and insert it to new table

    Quote Originally Posted by jmcilhinney View Post
    If you populate a DataTable at the start and bind that, then update the existing rows in that DataTable rather than removing the existing rows and adding new ones or creating a whole new DataTable then yes, the other data will remain unaffected.
    Yes I populate a datatable and bind it to datagridview that consist of 3 columns only then i added 2 columns that is not in datatable, I just add it manually name(TARGET & BALANCE) in Target col user will input qty and in Balance col it will subtract the Target to Input from datatable column.
    how can i load it again without affecting the 2 added column if all are located in one grid view ?
    Last edited by BONITO; Jul 4th, 2019 at 01:54 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width