Skip to content
The Chat Archive
gpt-4-0314· OpenAI WildChat-4.8M (AI2)

the code you gave me throws this error :System.ArgumentException: 'Column named…

  1. User

    the code you gave me throws this error :System.ArgumentException: 'Column named Appliance cannot be found. line where error occurs :string applianceInGrid = dataGridView1.Rows[i].Cells["Appliance"].Value.ToString();

    rest of my code: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Runtime.Remoting.Lifetime; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows.Forms; using static System.Net.Mime.MediaTypeNames; using static System.Windows.Forms.VisualStyles.VisualStyleElement; using System.Text.RegularExpressions; using static System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox;

    namespace ApplianceRental { public partial class CustomerDashboardForm : Form { private DataTable cartItems = new DataTable(); public CustomerDashboardForm() { InitializeComponent(); cartItems.Columns.Add("Appliance"); cartItems.Columns.Add("PowerUsage"); cartItems.Columns.Add("TypicalUsage"); cartItems.Columns.Add("AnnualCost"); // Set dataGridViewCart DataSource dataGridViewCart.DataSource = cartItems; }

        private void CustomerDashboardForm_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'db_usersDataSet.ApplianceDBLIST' table. You can move, or remove it, as needed.
            this.applianceDBLISTTableAdapter.Fill(this.db_usersDataSet.ApplianceDBLIST);
            BindingSource bindingSource = new BindingSource();
            bindingSource.DataSource = this.db_usersDataSet.ApplianceDBLIST;
            comboBox1.DataSource = bindingSource;
            comboBox1.DisplayMember = "Appliance";  // replace “Appliance” with your actual appliance column name
        }
    
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
    
        }
    
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                string applianceInGrid = dataGridView1.Rows[i].Cells["Appliance"].Value.ToString();
                string applianceInComboBox = comboBox1.SelectedItem.ToString();
    
                if (applianceInGrid == applianceInComboBox)
                {
                    dataGridView1.ClearSelection();
                    dataGridView1.Rows[i].Selected = true;
                    dataGridView1.CurrentCell = dataGridView1.Rows[i].Cells[0];
                    break;
                }
            }
            }
        private void CalculateTotal()
        {
            decimal totalAmount = 0;
    
            foreach (DataRow row in cartItems.Rows)
            {
                totalAmount += Convert.ToDecimal(row["AnnualCost"]);
            }
    
            labelTotalAmount.Text = "Total Amount: " + totalAmount.ToString("C");
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count < 1)
            {
                MessageBox.Show("Please select an appliance to add to the cart.");
                
            }
            // Get the selected row in dataGridView1
            DataGridViewRow selectedRow = dataGridView1.SelectedRows[0];
    
            // Create a new row for cartItems
            DataRow newRow = cartItems.NewRow();
    
            newRow["Appliance"] = selectedRow.Cells["Appliance"].Value;
            newRow["Power Usage"] = selectedRow.Cells["Power Usage"].Value;
            newRow["Typical Usage"] = selectedRow.Cells["Typical Usage"].Value;
            newRow["Estimated Annual Costs"] = selectedRow.Cells["Estimated annual running costs"].Value;
    
            // Add newRow to the cartItems
            cartItems.Rows.Add(newRow);
    
            // Calculate and display the total amount
            CalculateTotal();
        }
       
        private void searchBox_TextChanged(object sender, EventArgs e)
        {
        }
    
        private void button2_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(searchBox.Text))
            {
                MessageBox.Show("Please enter something to search");
            }
            else
            {
                DataView dataView = db_usersDataSet.ApplianceDBLIST.DefaultView;
                string searchExpression = Regex.Escape(searchBox.Text.Trim()).Replace("'", "''");
                dataView.RowFilter = string.Format("Appliance LIKE '%{0}%'", searchExpression);
                dataGridView1.DataSource = dataView;
            }
        }
    }
    

    }

  2. Assistant

    It seems that the column names in your DataGridView do not exactly match the ones in your DataTable. Also, make sure you are referring to the correct DataGridView: dataGridView1 for the appliances list and dataGridViewCart for the cart items list.

    Here's my suggestion to fix the issue:

    1. In the CustomerDashboardForm_Load method, after setting the BindingSource, set the DataGridView's DataSource as well:
    dataGridView1.DataSource = bindingSource;
    
    1. Update the column names in dataGridView1 (the appliances list) to exactly match the ones in your DataTable. To do this, go through the DataGridView's properties in the DataGridView's design view, and update the column names in the Columns section.

    2. Update the column names in button1_Click method to match the updated column names in dataGridView1:

    newRow["Appliance"] = selectedRow.Cells["Appliance"].Value;
    newRow["PowerUsage"] = selectedRow.Cells["Power Usage"].Value; // Update the column name
    newRow["TypicalUsage"] = selectedRow.Cells["Typical Usage"].Value; // Update the column name
    newRow["AnnualCost"] = selectedRow.Cells["Estimated Annual Costs"].Value; // Update the column name
    

    Give this a try and let me know if it resolves the issue.

  3. User

    it doesnt I still get the same error: System.ArgumentException: 'Column named Appliance cannot be found.

    my code: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Runtime.Remoting.Lifetime; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows.Forms; using static System.Net.Mime.MediaTypeNames; using static System.Windows.Forms.VisualStyles.VisualStyleElement; using System.Text.RegularExpressions; using static System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox;

    namespace ApplianceRental { public partial class CustomerDashboardForm : Form { private DataTable cartItems = new DataTable(); public CustomerDashboardForm() { InitializeComponent(); cartItems.Columns.Add("Appliance"); cartItems.Columns.Add("PowerUsage"); cartItems.Columns.Add("TypicalUsage"); cartItems.Columns.Add("AnnualCost"); // Set dataGridViewCart DataSource dataGridViewCart.DataSource = cartItems; }

        private void CustomerDashboardForm_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'db_usersDataSet.ApplianceDBLIST' table. You can move, or remove it, as needed.
            this.applianceDBLISTTableAdapter.Fill(this.db_usersDataSet.ApplianceDBLIST);
            BindingSource bindingSource = new BindingSource();
            bindingSource.DataSource = this.db_usersDataSet.ApplianceDBLIST;
            comboBox1.DataSource = bindingSource;
            comboBox1.DisplayMember = "Appliance";  // replace “Appliance” with your actual appliance column name
            dataGridView1.DataSource = bindingSource;
        }
    
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
    
        }
    
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                string applianceInGrid = dataGridView1.Rows[i].Cells["Appliance"].Value.ToString();
                string applianceInComboBox = comboBox1.SelectedItem.ToString();
    
                if (applianceInGrid == applianceInComboBox)
                {
                    dataGridView1.ClearSelection();
                    dataGridView1.Rows[i].Selected = true;
                    dataGridView1.CurrentCell = dataGridView1.Rows[i].Cells[0];
                    break;
                }
            }
            }
        private void CalculateTotal()
        {
            decimal totalAmount = 0;
    
            foreach (DataRow row in cartItems.Rows)
            {
                totalAmount += Convert.ToDecimal(row["AnnualCost"]);
            }
    
            labelTotalAmount.Text = "Total Amount: " + totalAmount.ToString("C");
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count < 1)
            {
                MessageBox.Show("Please select an appliance to add to the cart.");
                
            }
            // Get the selected row in dataGridView1
            DataGridViewRow selectedRow = dataGridView1.SelectedRows[0];
    
            // Create a new row for cartItems
            DataRow newRow = cartItems.NewRow();
    
            newRow["Appliance"] = selectedRow.Cells["Appliance"].Value;
            newRow["PowerUsage"] = selectedRow.Cells["Power Usage"].Value; // Update the column name
            newRow["TypicalUsage"] = selectedRow.Cells["Typical Usage"].Value; // Update the column name
            newRow["AnnualCost"] = selectedRow.Cells["Estimated Annual Costs"].Value; // Update the column name
    
            // Add newRow to the cartItems
            cartItems.Rows.Add(newRow);
    
            // Calculate and display the total amount
            CalculateTotal();
        }
       
        private void searchBox_TextChanged(object sender, EventArgs e)
        {
        }
    
        private void button2_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(searchBox.Text))
            {
                MessageBox.Show("Please enter something to search");
            }
            else
            {
                DataView dataView = db_usersDataSet.ApplianceDBLIST.DefaultView;
                string searchExpression = Regex.Escape(searchBox.Text.Trim()).Replace("'", "''");
                dataView.RowFilter = string.Format("Appliance LIKE '%{0}%'", searchExpression);
                dataGridView1.DataSource = dataView;
            }
        }
    }
    

    }

  4. Assistant

    My apologies for the confusion earlier. Let's modify the approach slightly: Instead of referring to the columns using their name, let's refer to them using their index in the DataGridView.

    In the comboBox1_SelectedIndexChanged method, change this line:

    string applianceInGrid = dataGridView1.Rows[i].Cells["Appliance"].Value.ToString();
    

    to

    string applianceInGrid = dataGridView1.Rows[i].Cells[0].Value.ToString(); // Assuming Appliance is the first column
    

    And in the button1_Click method, change these lines:

    newRow["PowerUsage"] = selectedRow.Cells["Power Usage"].Value;
    newRow["TypicalUsage"] = selectedRow.Cells["Typical Usage"].Value;
    newRow["AnnualCost"] = selectedRow.Cells["Estimated Annual Costs"].Value;
    

    to

    newRow["PowerUsage"] = selectedRow.Cells[1].Value; // Assuming Power Usage is the second column
    newRow["TypicalUsage"] = selectedRow.Cells[2].Value; // Assuming Typical Usage is the third column
    newRow["AnnualCost"] = selectedRow.Cells[3].Value; // Assuming Estimated Annual Costs is the fourth column
    

    Please give this a try and let me know if this resolves the issue.

  5. User

    still have this error : System.NullReferenceException: 'Object reference not set to an instance of an object.' from this line : string applianceInGrid = dataGridView1.Rows[i].Cells[0].Value.ToString();

    my code: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Runtime.Remoting.Lifetime; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows.Forms; using static System.Net.Mime.MediaTypeNames; using static System.Windows.Forms.VisualStyles.VisualStyleElement; using System.Text.RegularExpressions; using static System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox;

    namespace ApplianceRental { public partial class CustomerDashboardForm : Form { private DataTable cartItems = new DataTable(); public CustomerDashboardForm() { InitializeComponent(); cartItems.Columns.Add("Appliance"); cartItems.Columns.Add("PowerUsage"); cartItems.Columns.Add("TypicalUsage"); cartItems.Columns.Add("AnnualCost"); // Set dataGridViewCart DataSource dataGridViewCart.DataSource = cartItems; }

        private void CustomerDashboardForm_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'db_usersDataSet.ApplianceDBLIST' table. You can move, or remove it, as needed.
            this.applianceDBLISTTableAdapter.Fill(this.db_usersDataSet.ApplianceDBLIST);
            BindingSource bindingSource = new BindingSource();
            bindingSource.DataSource = this.db_usersDataSet.ApplianceDBLIST;
            comboBox1.DataSource = bindingSource;
            comboBox1.DisplayMember = "Appliance";  // replace “Appliance” with your actual appliance column name
            dataGridView1.DataSource = bindingSource;
        }
    
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
    
        }
    
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                string applianceInGrid = dataGridView1.Rows[i].Cells[0].Value.ToString(); // Assuming Appliance is the first column
                string applianceInComboBox = comboBox1.SelectedItem.ToString();
    
                if (applianceInGrid == applianceInComboBox)
                {
                    dataGridView1.ClearSelection();
                    dataGridView1.Rows[i].Selected = true;
                    dataGridView1.CurrentCell = dataGridView1.Rows[i].Cells[0];
                    break;
                }
            }
            }
        private void CalculateTotal()
        {
            decimal totalAmount = 0;
    
            foreach (DataRow row in cartItems.Rows)
            {
                totalAmount += Convert.ToDecimal(row["AnnualCost"]);
            }
    
            labelTotalAmount.Text = "Total Amount: " + totalAmount.ToString("C");
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count < 1)
            {
                MessageBox.Show("Please select an appliance to add to the cart.");
                
            }
            // Get the selected row in dataGridView1
            DataGridViewRow selectedRow = dataGridView1.SelectedRows[0];
    
            // Create a new row for cartItems
            DataRow newRow = cartItems.NewRow();
    
            newRow["Appliance"] = selectedRow.Cells["Appliance"].Value;
            newRow["PowerUsage"] = selectedRow.Cells[1].Value; // Assuming Power Usage is the second column
            newRow["TypicalUsage"] = selectedRow.Cells[2].Value; // Assuming Typical Usage is the third column
            newRow["AnnualCost"] = selectedRow.Cells[3].Value; // Assuming Estimated Annual Costs is the fourth column
            // Add newRow to the cartItems
            cartItems.Rows.Add(newRow);
    
            // Calculate and display the total amount
            CalculateTotal();
        }
       
        private void searchBox_TextChanged(object sender, EventArgs e)
        {
        }
    
        private void button2_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(searchBox.Text))
            {
                MessageBox.Show("Please enter something to search");
            }
            else
            {
                DataView dataView = db_usersDataSet.ApplianceDBLIST.DefaultView;
                string searchExpression = Regex.Escape(searchBox.Text.Trim()).Replace("'", "''");
                dataView.RowFilter = string.Format("Appliance LIKE '%{0}%'", searchExpression);
                dataGridView1.DataSource = dataView;
            }
        }
    }
    

    }

  6. Assistant

    It appears the issue is caused by trying to access the Value property of the cells when it is null. To fix this error, we should check if the Value is not null before trying to call ToString() on it.

    Update the loop in comboBox1_SelectedIndexChanged method to add the null check:

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        for (int i = 0; i < dataGridView1.Rows.Count; i++)
        {
            if (dataGridView1.Rows[i].Cells[0].Value != null)  // Add this line to check for null values
            {
                string applianceInGrid = dataGridView1.Rows[i].Cells[0].Value.ToString(); // Assuming Appliance is the first column
                string applianceInComboBox = comboBox1.SelectedItem.ToString();
    
                if (applianceInGrid == applianceInComboBox)
                {
                    dataGridView1.ClearSelection();
                    dataGridView1.Rows[i].Selected = true;
                    dataGridView1.CurrentCell = dataGridView1.Rows[i].Cells[0];
                    break;
                }
            }
        }
    }
    

    This should resolve the NullReferenceException issue.

  7. User

    ok cool now I want that the drop down menu of appliances in the combo box when appliance selected and addtocart button is pressed I want it to be added to cart

    my code: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Runtime.Remoting.Lifetime; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows.Forms; using static System.Net.Mime.MediaTypeNames; using static System.Windows.Forms.VisualStyles.VisualStyleElement; using System.Text.RegularExpressions; using static System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox;

    namespace ApplianceRental { public partial class CustomerDashboardForm : Form { private DataTable cartItems = new DataTable(); public CustomerDashboardForm() { InitializeComponent(); cartItems.Columns.Add("Appliance"); cartItems.Columns.Add("PowerUsage"); cartItems.Columns.Add("TypicalUsage"); cartItems.Columns.Add("AnnualCost"); // Set dataGridViewCart DataSource dataGridViewCart.DataSource = cartItems; }

        private void CustomerDashboardForm_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'db_usersDataSet.ApplianceDBLIST' table. You can move, or remove it, as needed.
            this.applianceDBLISTTableAdapter.Fill(this.db_usersDataSet.ApplianceDBLIST);
            BindingSource bindingSource = new BindingSource();
            bindingSource.DataSource = this.db_usersDataSet.ApplianceDBLIST;
            comboBox1.DataSource = bindingSource;
            comboBox1.DisplayMember = "Appliance";  // replace “Appliance” with your actual appliance column name
            dataGridView1.DataSource = bindingSource;
        }
    
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
    
        }
    
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (dataGridView1.Rows[i].Cells[0].Value != null)  // Add this line to check for null values
                {
                    string applianceInGrid = dataGridView1.Rows[i].Cells[0].Value.ToString(); // Assuming Appliance is the first column
                    string applianceInComboBox = comboBox1.SelectedItem.ToString();
    
                    if (applianceInGrid == applianceInComboBox)
                    {
                        dataGridView1.ClearSelection();
                        dataGridView1.Rows[i].Selected = true;
                        dataGridView1.CurrentCell = dataGridView1.Rows[i].Cells[0];
                        break;
                    }
                }
                }
            }
        private void CalculateTotal()
        {
            decimal totalAmount = 0;
    
            foreach (DataRow row in cartItems.Rows)
            {
                totalAmount += Convert.ToDecimal(row["AnnualCost"]);
            }
    
            labelTotalAmount.Text = "Total Amount: " + totalAmount.ToString("C");
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count < 1)
            {
                MessageBox.Show("Please select an appliance to add to the cart.");
                
            }
            // Get the selected row in dataGridView1
            DataGridViewRow selectedRow = dataGridView1.SelectedRows[0];
    
            // Create a new row for cartItems
            DataRow newRow = cartItems.NewRow();
    
            newRow["Appliance"] = selectedRow.Cells["Appliance"].Value;
            newRow["PowerUsage"] = selectedRow.Cells[1].Value; // Assuming Power Usage is the second column
            newRow["TypicalUsage"] = selectedRow.Cells[2].Value; // Assuming Typical Usage is the third column
            newRow["AnnualCost"] = selectedRow.Cells[3].Value; // Assuming Estimated Annual Costs is the fourth column
            // Add newRow to the cartItems
            cartItems.Rows.Add(newRow);
    
            // Calculate and display the total amount
            CalculateTotal();
        }
       
        private void searchBox_TextChanged(object sender, EventArgs e)
        {
        }
    
        private void button2_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(searchBox.Text))
            {
                MessageBox.Show("Please enter something to search");
            }
            else
            {
                DataView dataView = db_usersDataSet.ApplianceDBLIST.DefaultView;
                string searchExpression = Regex.Escape(searchBox.Text.Trim()).Replace("'", "''");
                dataView.RowFilter = string.Format("Appliance LIKE '%{0}%'", searchExpression);
                dataGridView1.DataSource = dataView;
            }
        }
    }
    

    }

  8. Assistant

    To achieve this, you have already set up the logic to add the selected appliance to the cart when the "Add to Cart" button is clicked. However, the current implementation is tied to the selection in the DataGridView (dataGridView1).

    To use the selected index of the comboBox1, you can simply modify your button1_Click function. Replace the following line:

    DataGridViewRow selectedRow = dataGridView1.SelectedRows[0];
    

    with:

    DataGridViewRow selectedRow = dataGridView1.Rows[comboBox1.SelectedIndex];
    

    This way, when the "Add to Cart" button is clicked, it will look for the appliance in the dataGridView1 based on the comboBox1 selection.

    Here's the updated code for the button1_Click method:

    private void button1_Click(object sender, EventArgs e)
    {
        if (comboBox1.SelectedIndex < 0)
        {
            MessageBox.Show("Please select an appliance to add to the cart.");
        }
        else
        {
            // Get the selected row in dataGridView1
            DataGridViewRow selectedRow = dataGridView1.Rows[comboBox1.SelectedIndex];
    
            // Create a new row for cartItems
            DataRow newRow = cartItems.NewRow();
    
            newRow["Appliance"] = selectedRow.Cells["Appliance"].Value;
            newRow["PowerUsage"] = selectedRow.Cells[1].Value; // Assuming Power Usage is the second column
            newRow["TypicalUsage"] = selectedRow.Cells[2].Value; // Assuming Typical Usage is the third column
            newRow["AnnualCost"] = selectedRow.Cells[3].Value; // Assuming Estimated Annual Costs is the fourth column
    
            // Add newRow to the cartItems
            cartItems.Rows.Add(newRow);
    
            // Calculate and display the total amount
            CalculateTotal();
        }
    }
    

    Now, when you select an appliance from comboBox1 and click the "Add to Cart" button, it will be added to the cart table (dataGridViewCart).

  9. User

    it does not work I get this error : System.ArgumentException: 'Column named Appliance cannot be found. from this line : newRow["Appliance"] = selectedRow.Cells["Appliance"].Value;

    my code: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Runtime.Remoting.Lifetime; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows.Forms; using static System.Net.Mime.MediaTypeNames; using static System.Windows.Forms.VisualStyles.VisualStyleElement; using System.Text.RegularExpressions; using static System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox;

    namespace ApplianceRental { public partial class CustomerDashboardForm : Form { private DataTable cartItems = new DataTable(); public CustomerDashboardForm() { InitializeComponent(); cartItems.Columns.Add("Appliance"); cartItems.Columns.Add("PowerUsage"); cartItems.Columns.Add("TypicalUsage"); cartItems.Columns.Add("AnnualCost"); // Set dataGridViewCart DataSource dataGridViewCart.DataSource = cartItems; }

        private void CustomerDashboardForm_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'db_usersDataSet.ApplianceDBLIST' table. You can move, or remove it, as needed.
            this.applianceDBLISTTableAdapter.Fill(this.db_usersDataSet.ApplianceDBLIST);
            BindingSource bindingSource = new BindingSource();
            bindingSource.DataSource = this.db_usersDataSet.ApplianceDBLIST;
            comboBox1.DataSource = bindingSource;
            comboBox1.DisplayMember = "Appliance";  // replace “Appliance” with your actual appliance column name
            dataGridView1.DataSource = bindingSource;
        }
    
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
    
        }
    
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (dataGridView1.Rows[i].Cells[0].Value != null)  // Add this line to check for null values
                {
                    string applianceInGrid = dataGridView1.Rows[i].Cells[0].Value.ToString(); // Assuming Appliance is the first column
                    string applianceInComboBox = comboBox1.SelectedItem.ToString();
    
                    if (applianceInGrid == applianceInComboBox)
                    {
                        dataGridView1.ClearSelection();
                        dataGridView1.Rows[i].Selected = true;
                        dataGridView1.CurrentCell = dataGridView1.Rows[i].Cells[0];
                        break;
                    }
                }
                }
            }
        private void CalculateTotal()
        {
            decimal totalAmount = 0;
    
            foreach (DataRow row in cartItems.Rows)
            {
                totalAmount += Convert.ToDecimal(row["AnnualCost"]);
            }
    
            labelTotalAmount.Text = "Total Amount: " + totalAmount.ToString("C");
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex < 0)
            {
                MessageBox.Show("Please select an appliance to add to the cart.");
            }
            else
            {
                // Get the selected row in dataGridView1
                DataGridViewRow selectedRow = dataGridView1.Rows[comboBox1.SelectedIndex];
    
                // Create a new row for cartItems
                DataRow newRow = cartItems.NewRow();
    
                newRow["Appliance"] = selectedRow.Cells["Appliance"].Value;
                newRow["PowerUsage"] = selectedRow.Cells[1].Value; // Assuming Power Usage is the second column
                newRow["TypicalUsage"] = selectedRow.Cells[2].Value; // Assuming Typical Usage is the third column
                newRow["AnnualCost"] = selectedRow.Cells[3].Value; // Assuming Estimated Annual Costs is the fourth column
    
                // Add newRow to the cartItems
                cartItems.Rows.Add(newRow);
    
                // Calculate and display the total amount
                CalculateTotal();
            }
        }
    
            private void searchBox_TextChanged(object sender, EventArgs e)
        {
        }
    
        private void button2_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(searchBox.Text))
            {
                MessageBox.Show("Please enter something to search");
            }
            else
            {
                DataView dataView = db_usersDataSet.ApplianceDBLIST.DefaultView;
                string searchExpression = Regex.Escape(searchBox.Text.Trim()).Replace("'", "''");
                dataView.RowFilter = string.Format("Appliance LIKE '%{0}%'", searchExpression);
                dataGridView1.DataSource = dataView;
            }
        }
    }
    

    }

  10. Assistant

    I apologize for the confusion. The issue seems to be related to the column names once again. Let's refer to the columns using their index instead of their names:

    In the button1_Click method, replace this line:

    newRow["Appliance"] = selectedRow.Cells["Appliance"].Value;
    

    with:

    newRow["Appliance"] = selectedRow.Cells[0].Value;
    

    Here's the updated code for the button1_Click method:

    private void button1_Click(object sender, EventArgs e)
    {
        if (comboBox1.SelectedIndex < 0)
        {
            MessageBox.Show("Please select an appliance to add to the cart.");
        }
        else
        {
            // Get the selected row in dataGridView1
            DataGridViewRow selectedRow = dataGridView1.Rows[comboBox1.SelectedIndex];
    
            // Create a new row for cartItems
            DataRow newRow = cartItems.NewRow();
    
            newRow["Appliance"] = selectedRow.Cells[0].Value; // Use the column index instead of name
            newRow["PowerUsage"] = selectedRow.Cells[1].Value; // Assuming Power Usage is the second column
            newRow["TypicalUsage"] = selectedRow.Cells[2].Value; // Assuming Typical Usage is the third column
            newRow["AnnualCost"] = selectedRow.Cells[3].Value; // Assuming Estimated Annual Costs is the fourth column
    
            // Add newRow to the cartItems
            cartItems.Rows.Add(newRow);
    
            // Calculate and display the total amount
            CalculateTotal();
        }
    }
    

    This should resolve the error and add the selected appliance to the cart when the "Add to Cart" button is clicked.