Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
if (e.Key == Key.Enter)
           {
              // string query = "";
               dataGrid.Focus();

               //dataGrid.CurrentColumn= dataGrid.Columns[2];
               if (txtSearch.Text != "")
               {
                   if (txtSearch.Text != null && txtSearch.Text != "")
                   {
                       //this.DataContext = txtSearch.Text;
                       //dataGrid.ItemsSource = txtSearch.SelectedValue.ToString();
                       DataSet ds = Globalvariables.Globals.select_load("Select menu_id,foodname,rprice from add_food_menu where foodname like '%" + txtSearch.Text + "%'");
                       if (ds.Tables[0].Rows.Count > 0)
                       {
                           for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                           {
                               string id = ds.Tables[0].Rows[i][0].ToString();
                               string name = ds.Tables[0].Rows[i][1].ToString();
                               string price = ds.Tables[0].Rows[i][2].ToString();
                               string qty = "1";
                               string total = price;
                               //MenuList.Add(new menu(ds.Tables[0].Rows[i][0].ToString(), ds.Tables[0].Rows[i][1].ToString(), ds.Tables[0].Rows[i][2].ToString(), "1"));
                               dataGrid.DisplayMemberPath = "foodname";
                               dataGrid.SelectedValuePath = "menu_id";
                               //dataGrid.ItemsSource = ds.Tables[0].DefaultView;
                               dataGrid.Items.Add(new menu(id = ds.Tables[0].Rows[i][0].ToString(), name = ds.Tables[0].Rows[i][1].ToString(), price = ds.Tables[0].Rows[i][2].ToString(), "1",total));
                               dataGrid.BeginningEdit += (e, ee) => ee.Cancel = true;
                               dataGrid.CurrentCell = new DataGridCellInfo(
                               dataGrid.Items[0], dataGrid.Columns[2]);
                               dataGrid.BeginEdit();
                           }
                           //autoList = MenuList;
                       }
                   }
               }
           }
       }


What I have tried:

public class menu : INotifyPropertyChanged
      {
          public static string id;
          public static string name;
          public static string price;
          public static string qty;
          public static string total;

          public menu(string id, string name, string price,string qty,string total)
          {
              this.Id = id;
              this.Name = name;
              this.Price = price;
              this.Qty = qty;
              this.Total = total;
          }
          public string Id
          {
              get { return id; }
              set { id = value; }
          }
          public string Name
          {
              get { return name; }
              set { name = value; }
          }
          public string Price
          {
              get { return price; }
              set { price = value; }
          }
          public string Qty
          {
              get { return qty; }
              set { qty = value;  if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Qty")); }
          }

          public string Total
          {
              get { return (float.Parse(this.Qty) * float.Parse(this.Price)).ToString(); }
              set { total = value; if(PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Total")); }
          }

          private void NotifyPropertyChanged(string total)
          {
              if(PropertyChanged != null)
              {
                  PropertyChanged(this, new PropertyChangedEventArgs(total));
              }
          }
          public event PropertyChangedEventHandler PropertyChanged;
          public class UpdateData
          {
              public ObservableCollection<menu> updata { get; set; }
              public UpdateData()
              {
                  updata = new ObservableCollection<menu>();
              }
          }
Posted
Comments
Graeme_Grant 23-Nov-22 4:47am    
You have asked over 30 questions in the last couple of months. You should know by now how to post the code and discuss your issue correctly.

As for the code, it's quite unruly. You are throwing two totally different methodologies together hoping that something works. Please, take the time to learn how to use data binding correctly: Data binding overview - WPF .NET | Microsoft Learn[^] or Introduction to WPF data binding - The complete WPF tutorial[^] OR Bind WPF controls to a dataset - Visual Studio (Windows) | Microsoft Learn[^]... otherwise you are wasting your time and ours.
Richard Deeming 23-Nov-22 4:54am    
DataSet ds = Globalvariables.Globals.select_load("Select menu_id,foodname,rprice from add_food_menu where foodname like '%" + txtSearch.Text + "%'");

Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation/interpolation to build a SQL query. ALWAYS use a parameterized query.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900