Solved

update value in a DataGridView c#

Posted on 2010-09-02
3
760 Views
Last Modified: 2012-05-10
I am trying to upate particular cells in a grid, The gird is already populated with values, and i have a button that I want to update every row on the grid , but only a particular cell in the grid

Here is what I have so far but I am getting stuck on how to update the data on the grid ...

        private void ApplyAllButton_Click
                    (object sender
                    ,EventArgs e
                    ,string lv_CustomerID)
        {
            int rownum = 0;
            string temp_msg = "";

            Dictionary<string, string> UpdateValues;
            UpdateValues.Add("SalesPrice", "");
            UpdateValues.Add("BasePrice", "");
            UpdateValues.Add("DiscountPerc", "");
            UpdateValues.Add("DiscountType", "");
            UpdateValues.Add("Authorized", "");
            UpdateValues.Add("NetPrice", "");

            dataGridView1 = SaveLocal(dataGridView1, MacStockCheckDic.Stock_check_columns());

            if (g_debug_level > 1) { temp_msg = "oracleProcedure"; }

            //if (CustomerID.Text == String.Empty)
            if (lv_CustomerID == "")
            {
                MessageBox.Show("No Customer ID has been entered, customer information related \n"
                                +"to the parts you have chosen will not be updated.\n"
                                +"Please check to make sure you have a Valid Customer ID. \n"
                                 , "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                //do a check to see if the customer exists in IFS
            }
            else
            {
                foreach (DataGridViewRow GridRow in dataGridView1.Rows)
                {
                    //get the data from the row that you need to update into a dictionary
                    foreach (KeyValuePair<string, string> MACViewColsPair in MACVW_STOCK_CHECK_ColDictionary)
                    {
                        StockCheckColDictionary[MACViewColsPair.Key] = GridRow.Cells[MACViewColsPair.Value].ToString();
                    }

                    // update the dictionary with the updated values
                    MacOraIFS.UpdateStockCheckColumns_from_MAC_PRICING_API_Get_Price
                               (StockCheckColDictionary
                               , dbenviroment
                               , lv_CustomerID
                               , "10"
                               , OracleDbConn);
 
                    //update the grid cells based on the values i have in the dictionary
                    // this is where i have the problem
                    // the UpdateValues Dictionary has the coloumn names of the grid that I want to
                   // update..
                   foreach (KeyValuePair<string, string> UpdateValuesPair in UpdateValues)
                    {
                        GridRow.Cells[UpdateValuesPair.Key] = "1";
                            //StockCheckColDictionary[UpdateValuesPair.Key];
                    }


                }
            }

        }
0
Comment
Question by:jhacharya
  • 2
3 Comments
 
LVL 16

Expert Comment

by:kris_per
ID: 33590130

What is in 'MACVW_STOCK_CHECK_ColDictionary'?
0
 
LVL 2

Accepted Solution

by:
jhacharya earned 0 total points
ID: 33591732
I have got a completed version which complies if anyone wants to use it, the MACVW_STOCK_CHECKColDictionary, is a dictionary of the column names that I was interested in

        public void UpdateDataGridRow
                (Dictionary<string, string> lv_StockCheckGridCols
                , Dictionary<string, string> lv_ColumnFormatGridViewDictionary
                , Dictionary<string, string> lv_ColumnsUpdatedGridViewDictionary
                , DataGridViewRow lv_GridRow)
        {
            try
            {
                // this is to populate the data table and bind to the datagrid
                Decimal TempValueCurrency;
                DateTime TempValueDate;

                foreach (KeyValuePair<string, string> UpdateValuesPair in lv_ColumnsUpdatedGridViewDictionary)
                {
                    if (lv_StockCheckGridCols[UpdateValuesPair.Key] != "")
                    {
                        switch (lv_ColumnFormatGridViewDictionary[UpdateValuesPair.Key])
                        {
                            case "N":
                                {
                                    TempValueCurrency = Convert.ToDecimal(lv_StockCheckGridCols[UpdateValuesPair.Key]);
                                    lv_GridRow.Cells[UpdateValuesPair.Key].Value = TempValueCurrency.ToString("c");
                                    break;
                                }
                            case "D":
                                {
                                    //TempValueDate = Convert.ToDateTime(StockCheckPair.Value);
                                    //myNewRow[StockCheckPair.Key] = TempValueDate.ToString("c");
                                    lv_GridRow.Cells[UpdateValuesPair.Key].Value = lv_StockCheckGridCols[UpdateValuesPair.Key];
                                    break;
                                }

                            case "S":
                                {
                                    lv_GridRow.Cells[UpdateValuesPair.Key].Value = lv_StockCheckGridCols[UpdateValuesPair.Key];
                                    break;
                                }
                            default:
                                {
                                    lv_GridRow.Cells[UpdateValuesPair.Key].Value = lv_StockCheckGridCols[UpdateValuesPair.Key];
                                    break;
                                }
                        }
                    }
                    else
                    {
                        lv_GridRow.Cells[UpdateValuesPair.Key].Value = lv_StockCheckGridCols[UpdateValuesPair.Key];
                    }
                }
            }
            catch (Exception err)
            {
                Console.WriteLine("<b>Message:</b> " + err.Message
                                  + "<br /><br />"
                                  + "<b>Source:</b> " + err.Source
                                  + "<br /><br />"
                                  + "<b>Stack Trace:</b> " + err.StackTrace);
            }

        }//public void UpdateDataGridRow
0
 
LVL 2

Author Closing Comment

by:jhacharya
ID: 33591739
this procedure updates particular columns on the  row of the grid view in Win forms
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now