简体   繁体   English

如何将DataGridView中的单元格转换为按钮?

[英]How do I convert a cell in the DataGridView into a button?

i know how to use edit,delete,update in grid view(web application) but now i am working on windows form. 我知道如何在网格视图(Web应用程序)中使用编辑,删除,更新,但是现在我正在Windows窗体上工作。 i dont know how to use same edit delete update in winforms. 我不知道如何在Winforms中使用相同的编辑删除更新。

my code: 我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Text.RegularExpressions;

namespace Ata_Sk_Mae_System
{
    public partial class Stock_Details : Form
    {
        public Stock_Details()
        {
            InitializeComponent();


            //Set Columns Count
            gvItem.ColumnCount = 4;

            gvItem.AutoGenerateColumns = false;

            //Hide the last blank line
            gvItem.AllowUserToAddRows = false;

            //Add Columns
            gvItem.Columns[0].Name = "ICode";
            gvItem.Columns[0].HeaderText = "Item Code";
            gvItem.Columns[0].DataPropertyName = "ICode";
            gvItem.Columns[0].Width = 100;

            gvItem.Columns[1].HeaderText = "IName";
            gvItem.Columns[1].Name = "Item Name";
            gvItem.Columns[1].DataPropertyName = "IName";
            gvItem.Columns[1].Width = 100;

            gvItem.Columns[2].Name = "Iprice";
            gvItem.Columns[2].HeaderText = "Item Price";
            gvItem.Columns[2].DataPropertyName = "Iprice";
            gvItem.Columns[2].Width = 100;

            gvItem.Columns[3].Name = "Quantity";
            gvItem.Columns[3].HeaderText = "Quantity";
            gvItem.Columns[3].DataPropertyName = "Quantity";
            gvItem.Columns[3].Width = 100;

            DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn();
            checkBoxColumn.HeaderText = "";
            checkBoxColumn.Width = 30;
            checkBoxColumn.Name = "checkBoxColumn";
            gvItem.Columns.Insert(0, checkBoxColumn);



            BindGrid();
            timer1.Start();
        }

        DataTable dt;

        private void btnAdd_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "Data Source=LUCKY-PC;Initial Catalog=Test;User Id=sa;Password=admin@123";
            con.Open();
            string IC = txtItemCode.Text;
            string IN = txtItemName.Text;
            string IP = txtItemPrice.Text;
            string QTY = txtQuantity.Text;

            string query = "insert into Item_Details values('" + IC + "','" + IN + "','" + IP + "','" + QTY + "')";
            SqlCommand cmd = new SqlCommand(query, con);
            cmd.ExecuteNonQuery();
            MessageBox.Show("Item Added Succesfully");
            con.Close();
            BindGrid();
            btnReset.PerformClick();
        }

        private void BindGrid()
        {
            string constring = "Data Source=LUCKY-PC;Initial Catalog=Test;User Id=sa;Password=admin@123";
            using (SqlConnection con = new SqlConnection(constring))
            {
                using (SqlCommand cmd = new SqlCommand("SELECT distinct * FROM Item_Details", con))
                {
                    cmd.CommandType = CommandType.Text;
                    using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                    {
                        using (dt = new DataTable())
                        {
                            sda.Fill(dt);
                            gvItem.DataSource = dt;
                        }
                    }
                }
            }
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            txtItemCode.Text = "";
            txtItemName.Text = "";
            txtItemPrice.Text = "";
            txtQuantity.Text = "";
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            DateTime datetimer = DateTime.Now;
            this.lblDate_Time.Text = "Date && Time:" + "[" + datetimer.ToString() + "]";
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            List<DataGridViewRow> selectedRows = (from row in gvItem.Rows.Cast<DataGridViewRow>()
                                                  where Convert.ToBoolean(row.Cells["checkBoxColumn"].Value) == true
                                                  select row).ToList();
            if (MessageBox.Show(string.Format("Do you want to delete {0} rows?", selectedRows.Count), "Confirmation", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                foreach (DataGridViewRow row in selectedRows)
                {
                    using (SqlConnection con = new SqlConnection("Data Source=LUCKY-PC;Initial Catalog=Test;User Id=sa;Password=admin@123"))
                    {
                        using (SqlCommand cmd = new SqlCommand("DELETE FROM Item_Details WHERE ICode = @ICode", con))
                        {
                            cmd.CommandType = CommandType.Text;
                            cmd.Parameters.AddWithValue("@ICode", row.Cells["ICode"].Value);
                            con.Open();
                            cmd.ExecuteNonQuery();
                            con.Close();
                        }
                    }
                }

                this.BindGrid();
            }
        }

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            DataView dv = new DataView(dt);
            dv.RowFilter = string.Format("Icode Like '%{0}%'", txtSearch.Text);
            gvItem.DataSource = dv;
        }


    }
}

please anyone help me, how to add edit and update button in all rows and how to functioning them as well. 请任何人帮助我,如何在所有行中添加编辑和更新按钮,以及如何使它们起作用。 i am using sql server 2008 as database.thanks in advance. 我预先使用sql server 2008作为database.thanks。

Please look at the use of DataGridViewButton column for the visual studio version you are using. 请查看您正在使用的Visual Studio版本的DataGridViewButton列的使用。

https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewbuttoncolumn%28v=vs.110%29.aspx https://msdn.microsoft.com/zh-CN/library/system.windows.forms.datagridviewbuttoncolumn%28v=vs.110%29.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM