在C#中如何实现增删改查功能,就是点击相应按钮就可以在datagridview中实现相应的功能
3个回答
展开全部
winform不是asp.net,没有现成的Button给你用,只能自己加入Button列。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2012-01-05
展开全部
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
textBox2.Enabled = false;
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text.Trim() != "")
{
int i ;
for ( i = 0; i < dataGridView1.Rows.Count-1; i++)
if (dataGridView1.Rows[i].Cells[0].Value.ToString().Trim() == textBox1.Text.Trim())
break;
if (i >= dataGridView1.Rows.Count-1)
{
dataGridView1.Rows.Add(textBox1.Text.Trim());
}
else MessageBox.Show("记录已存在");
}
}
private void button3_Click(object sender, EventArgs e)
{
textBox2.Text = (dataGridView1.Rows.Count - 1).ToString();
}
private void button2_Click(object sender, EventArgs e)
{
int i ;
for ( i = 0; i < dataGridView1.Rows.Count-1; i++)
if (dataGridView1.Rows[i].Cells[0].Selected)
{ dataGridView1.Rows.RemoveAt(i); }
}
}
}
/*textbox1是添加,textbox2是删除,textbox3是统计,datagridview1是列表*/
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
textBox2.Enabled = false;
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text.Trim() != "")
{
int i ;
for ( i = 0; i < dataGridView1.Rows.Count-1; i++)
if (dataGridView1.Rows[i].Cells[0].Value.ToString().Trim() == textBox1.Text.Trim())
break;
if (i >= dataGridView1.Rows.Count-1)
{
dataGridView1.Rows.Add(textBox1.Text.Trim());
}
else MessageBox.Show("记录已存在");
}
}
private void button3_Click(object sender, EventArgs e)
{
textBox2.Text = (dataGridView1.Rows.Count - 1).ToString();
}
private void button2_Click(object sender, EventArgs e)
{
int i ;
for ( i = 0; i < dataGridView1.Rows.Count-1; i++)
if (dataGridView1.Rows[i].Cells[0].Selected)
{ dataGridView1.Rows.RemoveAt(i); }
}
}
}
/*textbox1是添加,textbox2是删除,textbox3是统计,datagridview1是列表*/
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询