0
Answer

Set a value in gridview textbox after selecting val combobox

Naveen Kumar

Naveen Kumar

9y
629
1

Hi I have tried a lot for this topic, but I didn't get it, please help me working on windows application...c#

whole idea is manually write the data in datagridview and insert the same in database. (Purchase details for POS) I have 4 columns in datagridview, 3rd column is combobox which contains productname, when I select value in productname from dropdown... productid should update automatically in 4th column which is textbox from List"ProductItem" . Here the issue is, when i select value in combobox not automatically updating the values in 4th column. I need your help cellleave method in the below program

public partial class Form1 : Form { AutoCompleteStringCollection scAutoComplete = new AutoCompleteStringCollection();

List productItems = new List();
 public Form1()
    {
        InitializeComponent();
        GetProductItems();       
    }

    private void GetProductItems()
    {
        productItems.Clear();
        productItems.Add(new ProductItem(0, ""));

        MySqlCommand cmd = new MySqlCommand("Select distinct productid,productname from retail_store.product;", con);
        MySqlDataReader dr;
        con.Open();
        dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            //scAutoComplete.Add(dr.GetString("productname"));
            productItems.Add(new ProductItem(dr.GetInt32("productid"), dr.GetString("productname")));
        }
        con.Close();

        ((DataGridViewComboBoxColumn)dataGridView1.Columns["Column3"]).ValueMember = "ProductId";
        ((DataGridViewComboBoxColumn)dataGridView1.Columns["Column3"]).DisplayMember = "ProductName";
        ((DataGridViewComboBoxColumn)dataGridView1.Columns["Column3"]).DataSource = productItems;
    }
    private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        if (dataGridView1.CurrentCell.ColumnIndex ==1 && e.Control is TextBox)
        {
            TextBox productNameBox = (TextBox)e.Control;
            productNameBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            productNameBox.AutoCompleteSource = AutoCompleteSource.CustomSource;
            productNameBox.AutoCompleteCustomSource = scAutoComplete;
        }
        else if  (e.Control is TextBox) {
            TextBox anyOtherBox = (TextBox)e.Control;
            anyOtherBox.AutoCompleteMode = AutoCompleteMode.None;
        }
        else if (dataGridView1.CurrentCell.ColumnIndex == 2 &&  e.Control is ComboBox){
            if (e.Control is DataGridViewComboBoxEditingControl)
            {
                ((ComboBox)e.Control).DropDownStyle = ComboBoxStyle.DropDown;
                ((ComboBox)e.Control).AutoCompleteSource = AutoCompleteSource.ListItems;
                ((ComboBox)e.Control).AutoCompleteMode = AutoCompleteMode.Suggest;
                ((ComboBox)e.Control).Validated -= new EventHandler(combo_Validated);
                ((ComboBox)e.Control).Validated += new EventHandler(combo_Validated);                          
            }
        }

        }

    private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
    {
        if (e.ColumnIndex == 3)
        {
            dataGridView1.NotifyCurrentCellDirty(true); //<== THIS LINE FIXES THE ISSUE
        }
    }

    void combo_Validated(object sender, EventArgs e)
    {
        Object selectedItem = ((ComboBox)sender).SelectedItem;
        DataGridViewComboBoxColumn col = (DataGridViewComboBoxColumn)dataGridView1.Columns[dataGridView1.CurrentCell.ColumnIndex];
        if (!String.IsNullOrEmpty(col.ValueMember))
            dataGridView1.CurrentCell.Value = GetPropValue(selectedItem, col.ValueMember);
        else
            dataGridView1.CurrentCell.Value = selectedItem;

    }

    public static object GetPropValue(object src, string propName)
    {
        if (src == null)
            return null;
        return src.GetType().GetProperty(propName).GetValue(src, null);
    }

    private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
    {

        {
             here i need your help....
        }



    }


}

public class ProductItem
{
    public ProductItem(int prodid, string prodname)
    {
        ProductId = prodid;
        ProductName = prodname;
    }

    int _productid;
    public int ProductId
    {
        get
        {
            return this._productid;
        }
        set
        {
            this._productid = value;
        }
    }
    string _productname;
    public string ProductName
    {
        get
        {
            return this._productname;
        }
        set
        {
            this._productname = value;
        }
    }
}