WPF使用foreach循环禁用Datagrid中重复的行

时间:2019-04-21 07:56:08

标签: c# wpf

我想检查未绑定的数据网格中的每一行,如果单元格值等于文本框值,那么不要添加行

使用foreach循环

现在的问题是,iam使用未绑定的数据网格,并且无法在foreach循环中使用ItemSource 如果我运行下面的代码,我会报错: System.InvalidCastException:'无法将类型为'MyMasrof'的对象转换为类型为'System.Data.DataRow'。”

这是我的代码:

 private void AddMaterial_btn_Click(object sender, RoutedEventArgs e)
        {
            if (Materials_Lookup.Text == null)
            {
                MessageBox.Show("Choose Item First");
            }
            else
            {
                if (Masrof_Grid.Items.Count > 0)
                {


                    foreach (System.Data.DataRow dr in Masrof_Grid.Items)
                    {

                        if (dr["Masrof_Material_Name"].ToString() == StoreName_txt.Text)
                        {
                            MessageBox.Show("Item Already Exist");
                        }
                        else
                        {
                            MySqlConnection conn2 = new MySqlConnection("DataSource='" + DoSomething.Server_Param + "';UserID='" + DoSomething.Uid_Param + "';Password='" + DoSomething.Password_Param + "';Database='" + DoSomething.Database_Param + "';PORT=3306;CHARSET=utf8");
                            conn2.Open();
                            MySqlCommand cmd = new MySqlCommand();
                            MySqlDataReader rd;
                            DataSet ds = new DataSet();

                            ds.Clear();
                            cmd.Connection = conn2;

                            cmd.CommandText = "SELECT id FROM Items where Item_Arabic_Name  ='" + Materials_Lookup.Text + "'";
                            rd = cmd.ExecuteReader();

                            if (rd.Read())
                            {

                                Masrof_Grid.Items.Add(new MyMasrof()
                                {
                                    Masrof_Material_Code = (rd["id"].ToString()),
                                    Masrof_Material_Name = Materials_Lookup.Text,
                                    Masrof_MAterial_QTY = QTY_txt.Text,
                                    Masrof_Material_Store = StoreName_txt.Text,
                                    MAterial_Curr_Balance = CurrBalance_txt.Text

                                });
                            }
                            else
                            {
                                MessageBox.Show("Cannot be found");
                            }


                            conn2.Close();
                        }
                    }
                }
                else
                {
                    MySqlConnection conn2 = new MySqlConnection("DataSource='" + DoSomething.Server_Param + "';UserID='" + DoSomething.Uid_Param + "';Password='" + DoSomething.Password_Param + "';Database='" + DoSomething.Database_Param + "';PORT=3306;CHARSET=utf8");
                    conn2.Open();
                    MySqlCommand cmd = new MySqlCommand();
                    MySqlDataReader rd;
                    DataSet ds = new DataSet();

                    ds.Clear();
                    cmd.Connection = conn2;

                    cmd.CommandText = "SELECT id FROM Items where Item_Arabic_Name  ='" + Materials_Lookup.Text + "'";
                    rd = cmd.ExecuteReader();

                    if (rd.Read())
                    {

                        Masrof_Grid.Items.Add(new MyMasrof()
                        {
                            Masrof_Material_Code = (rd["id"].ToString()),
                            Masrof_Material_Name = Materials_Lookup.Text,
                            Masrof_MAterial_QTY = QTY_txt.Text,
                            Masrof_Material_Store = StoreName_txt.Text,
                            MAterial_Curr_Balance = CurrBalance_txt.Text

                        });
                      //  decimal sum = 0m;
                      //  for (int i = 0; i < Masrof_Grid.Items.Count; i++)
                      //  {
                      //      sum += (decimal.Parse((Masrof_Grid.Columns[7].GetCellContent(Masrof_Grid.Items[i]) as TextBlock).Text));
                      //  }
                      ////  txtSumReal.Text = sum.ToString();
                    }
                    else
                    {
                        MessageBox.Show("cannot be found");
                    }


                    conn2.Close();
                }




            }




        }

0 个答案:

没有答案