我知道如何在网格视图(Web应用程序)中使用编辑,删除,更新,但现在我正在使用Windows窗体。我不知道如何在winforms中使用相同的编辑删除更新。
我的代码:
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;
}
}
}
请任何人帮助我,如何在所有行中添加编辑和更新按钮以及如何运行它们。我正在使用sql server 2008作为database.thanks提前。
答案 0 :(得分:0)
请查看您正在使用的Visual Studio版本的DataGridViewButton列的使用。