如何让我的datagridview代码在数据网格中工作

时间:2015-01-24 23:26:39

标签: c# wpf winforms datagrid

我使用datagridview创建了一个win表单。我可以在哪里插入更新删除 来自数据库的文章。

但我需要一个wpf而不是win form项目。问题出在wpf中你没有数据网格视图。

现在我尝试让它使用数据网格,但没有行选项。

我已经找到了如何使用此DgArtikel.ItemsSource = ds.Tables [0] .DefaultView;

从我的数据网格中加载wcf中的数据

但是如何让插件更新和删除按钮工作?

 public partial class Form1 : Form
    {

        ServiceReference1.Service1Client objService = new ServiceReference1.Service1Client(); // Add service reference



        public Form1()
        {

            InitializeComponent();

            showdata();

        }



        private void showdata()  // To show the data in the DataGridView
        {

            DataSet ds,ds2 = new DataSet();

            ds = objService.SelectUserDetails();
            ds2 = objService.SelectCombobox();
            dataGridView1.DataSource = ds.Tables[0];

            dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);


            ComboBoxCategorie.DataSource = ds2.Tables[0];
            ComboBoxCategorie.DisplayMember = "Categorie";


        }



        private void btnClear_Click(object sender, EventArgs e)
        {
            int i = dataGridView1.SelectedCells[0].RowIndex;

            textBoxArtikel.Text = dataGridView1.Rows[i].Cells[1].Value.ToString();

            textBoxOmschrijving.Text = dataGridView1.Rows[i].Cells[2].Value.ToString();

            textBoxVerkoopprijs.Text = dataGridView1.Rows[i].Cells[3].Value.ToString();

            textBoxInStock.Text = dataGridView1.Rows[i].Cells[4].Value.ToString();

            ComboBoxCategorie.SelectedValue = dataGridView1.Rows[i].Cells[5].Value.ToString();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {

            ServiceReference1.UserDetails objuserdetail = new ServiceReference1.UserDetails(); // Add type reference
          //  objuserdetail.UserID = count;
            objuserdetail.Artikel = textBoxArtikel.Text;

            objuserdetail.Omschrijving = textBoxOmschrijving.Text;

            objuserdetail.Verkoopprijs =  Convert.ToInt32(textBoxVerkoopprijs.Text);

            objuserdetail.Instock = Convert.ToInt32(textBoxInStock.Text);


           objuserdetail.Cat_id = ComboBoxCategorie.SelectedIndex;
            objService.InsertUserDetails(objuserdetail); // To insert the data


            showdata();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            ServiceReference1.UserDetails objuserdetail = new ServiceReference1.UserDetails();

            if (dataGridView1.Rows.Count > 1)
            {

                DataTable dt = new DataTable();

             //   objuserdetail.UserID = (int)dataGridView1.CurrentRow.Cells[0].Value;

                objService.DeleteUserDetails(objuserdetail); // To Delete the data

                showdata();

            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            ServiceReference1.UserDetails objuserdetail = new ServiceReference1.UserDetails();

            objuserdetail.Artikel_id = (int)dataGridView1.CurrentRow.Cells[0].Value;

            objuserdetail.Artikel = textBoxArtikel.Text;

            objuserdetail.Omschrijving = textBoxOmschrijving.Text;

            objuserdetail.Verkoopprijs = Convert.ToInt32(textBoxVerkoopprijs.Text);

            objuserdetail.Instock = Convert.ToInt32(textBoxInStock.Text);

            objService.UpdateRegistrationTable(objuserdetail); // To Update the Data

            showdata();

            textBoxArtikel.Text = "";

            textBoxOmschrijving.Text = "";

            textBoxVerkoopprijs.Text = "";

            textBoxInStock.Text = "";
        }
    }
}

0 个答案:

没有答案