Solved

update value in a DataGridView c#

Posted on 2010-09-02
3
767 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Vb.net dynamic formulas in runtime 11 74
Entity Framework 7 42
C# bracket error 3 30
Add different cell to otherwise similiar row 4 39
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

770 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