110,580
社区成员
发帖
与我相关
我的任务
分享
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;
namespace cj_sys
{
public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
}
string conn_str = "Server=WIN_TEST;Database=choujiang;uid=sa;pwd=dsd_dsdn";
string sql_str = "select cj_names as 品牌名称,cj_show_counts as 可抽奖数,cj_old_counts as 原抽奖数,cj_pic_path as 商标路径,cj_date as 加入时间 from cj_main where cj_flag=0 order by cj_date desc";
private SqlDataAdapter find_do;
private DataSet ds;
private void Form4_Load(object sender, EventArgs e)
{
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
SqlConnection conn = new SqlConnection(conn_str);
SqlCommand cmd = new SqlCommand(sql_str,conn);
find_do = new SqlDataAdapter();
find_do.SelectCommand = cmd;
ds = new DataSet();
find_do.Fill(ds,"rs");
dataGridView1.DataSource =ds.Tables["rs"];
dataGridView1.Columns[0].FillWeight = 50;
dataGridView1.Columns[1].FillWeight = 50;
dataGridView1.Columns[2].FillWeight = 50;
dataGridView1.Columns[3].FillWeight = 260;
dataGridView1.Columns[4].FillWeight = 70;
dataGridView1.Columns[2].ReadOnly = true;
dataGridView1.Columns[4].ReadOnly = true;
}
private void button1_Click(object sender, EventArgs e)
{
SqlCommandBuilder updates = new SqlCommandBuilder(find_do);
find_do.UpdateCommand = updates.GetUpdateCommand();
find_do.Update(ds,"rs");
dataGridView1.Update();
}
private void button2_Click(object sender, EventArgs e)
{
SqlCommandBuilder delete = new SqlCommandBuilder(find_do);
find_do.DeleteCommand = delete.GetDeleteCommand();
dataGridView1.Rows.Remove(dataGridView1.CurrentRow);
find_do.Update(ds, "rs");
dataGridView1.Update();
}
private void button3_Click(object sender, EventArgs e)
{
SqlCommandBuilder delete = new SqlCommandBuilder(find_do);
find_do.DeleteCommand = delete.GetDeleteCommand();
dataGridView1.Rows.Clear();
find_do.Update(ds, "rs");
dataGridView1.Update();
}
}
}
//delete data in [test_2] while remove each rows in dataGridView
private void btn_ClearTable_Click(object sender , EventArgs e)
{
conn.Open();
cb.DataAdapter = da;
cb.GetDeleteCommand();
if (dataGridView1.Rows.Count > 0)
foreach (DataGridViewRow dr in dataGridView1.Rows)
//look at here please
dataGridView1.Rows.Remove(dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex]);
da.Update(ds.Tables[0]);
conn.Close();
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace test_DeleteTableThroughDataGridView
{
public partial class Form1 : Form
{
public SqlDataAdapter da;
public SqlConnection conn;
public SqlCommand cmd;
public SqlCommandBuilder cb;
public DataSet ds;
public string CmdText_Select = "select * from [test_2]";
public Form1()
{
InitializeComponent();
conn = new SqlConnection("Data Source=LX-WORK;Initial Catalog=testdb;Integrated Security=True");
cmd = new SqlCommand();
cb = new SqlCommandBuilder();
da = new SqlDataAdapter();
ds = new DataSet();
}
private void Form1_Load(object sender , EventArgs e)
{
}
//delete data in [test_2] while remove each rows in dataGridView
private void btn_ClearTable_Click(object sender , EventArgs e)
{
conn.Open();
cb.DataAdapter = da;
cb.GetDeleteCommand();
if (dataGridView1.Rows.Count > 0)
foreach (DataGridViewRow dr in dataGridView1.Rows)
//look at here please dataGridView1.Rows.Remove(dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex]);
da.Update(ds.Tables[0]);
conn.Close();
}
//show data
private void btn_Show_Click(object sender , EventArgs e)
{
dataGridView1.DataSource = null;
conn.Open();
cmd.CommandText = CmdText_Select;
cmd.Connection = conn;
da.SelectCommand = cmd;
da.Fill(ds , "tb1");
dataGridView1.DataSource = ds.Tables[0];
conn.Close();
}
private void btn_ESC_Click(object sender , EventArgs e)
{
this.Close();
this.Dispose();
}
}
}
SqlCommandBuilder delete = new SqlCommandBuilder(find_do);
find_do.DeleteCommand = delete.GetDeleteCommand();
dataGridView.Datasource=null;
dataGridView1.Rows.Clear();
find_do.Update(ds, "rs");
dataGridView1.Update();