System.Windows.Forms.DataGridView"不包含"Bind"的定义,并且找不到可接受类型为"

qq_43235351 2019-02-18 05:03:14
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.Threading.Tasks;
using System.Windows.Forms;


namespace zuoye2
{
public partial class frmCHA : Form
{
public frmCHA()
{
InitializeComponent();
}

//连接数据库据
string str = "Data Source=074B8J5ZEZU39W3\\SQLEXPRES;Initial Catalog=AirQualityDB;Integrated Security=True";
SqlConnection conn = new SqlConnection();//申明数据库连接

//主窗体
private void frmCHA_Load(object sender, EventArgs e)
{
BindAirQualityInfo();
}

//查询
private void BTNCHA_Click(object sender, EventArgs e)
{



}

public void BindAirQualityInfo()
{

SqlConnection conn = new SqlConnection(str);//连接
string cha = txtBoxCHA01.Text.Trim();//sql语句
string sql = string.Format("select * from AirQualityInfo,StationInfo where like '%{0}%'", cha);

SqlDataAdapter adapter = new SqlDataAdapter(sql,conn);//数据适配器
DataSet ds = new DataSet();//数据集
try
{
adapter.Fill(ds,"stu");//填充数据
dataGridView1.DataSource = ds;//绑定数据源
dataGridView1.Bind();
}
catch (Exception ex)
{

MessageBox.Show(ex.Message);
}
}

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
BindAirQualityInfo();
}

private void txtBoxCHA01_TextChanged(object sender, EventArgs e)
{

}
}
}

...全文
497 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
exception92 2019-02-18
  • 打赏
  • 举报
回复
Winform中的datagridview不需要Bind(),asp.net 中的需要。
_小黑_ 2019-02-18
  • 打赏
  • 举报
回复
datagrid.datasource = 数据源
dbfC#DataGridView中的常用技巧 只列出技巧部分,后面会有补充 0(最基本的技巧). 获取某列中的某行(某单元格)中的内容 this.currentposition = this.dataGridView1.BindingContext [this.dataGridView1.DataSource, this.dataGridView1.DataMember].Position; bookContent = this.database.dataSet.Tables[0].Rows [this.currentposition][21].ToString().Trim(); MessageBox.Show(bookContent); 1、自定义列 //定义列宽 this.dataGridView1.Columns[0].Width = 80; this.dataGridView1.Columns[1].Width = 80; this.dataGridView1.Columns[2].Width = 180; this.dataGridView1.Columns[3].Width = 120; this.dataGridView1.Columns[4].Width = 120; Customize Cells and Columns in the Windows Forms DataGridView Control by Extending Their Behavior and Appearance Host Controls in Windows Forms DataGridView Cells 继承 DataGridViewTextBoxCell 类生成新的Cell类,然后再继承 DataGridViewColumn 生成新的Column类,并指定 CellTemplate为新的Cell类。新生成的Column便可以增加到DataGridView中去。 2、自动适应列宽 Programmatically Resize Cells to Fit Content in the Windows Forms DataGridView Control Samples: DataGridView.AutoSizeColumns( DataGridViewAutoSizeColumnCriteria.HeaderAndDisplayedRows); DataGridView.AutoSizeColumn( DataGridViewAutoSizeColumnCriteria.HeaderOnly, 2, false); DataGridView.AutoSizeRow( DataGridViewAutoSizeRowCriteria.Columns, 2, false); DataGridView.AutoSizeRows( DataGridViewAutoSizeRowCriteria.HeaderAndColumns, 0, dataGridView1.Rows.Count, false); 3、可以绑定并显示对象 Bind Objects to Windows Forms DataGridView Controls 4、可以改变表格线条风格 Change the Border and Gridline Styles in the Windows Forms DataGridView Control Samples: this.dataGridView1.GridColor = Color.BlueViolet; this.dataGridView1.BorderStyle = BorderStyle.Fixed3D; this.dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None; this.dataGridView1.RowHeadersBorderStyle = DataGridVie
c# DBF数据库导入导出实例 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.Odbc; using System.Data.SqlClient; namespace DbfExample { public partial class Form1 : Form { System.Data.Odbc.OdbcConnection conn; public Form1() { InitializeComponent(); } //导出数据 private void btnOut_Click(object sender, EventArgs e) { string connect = "Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=c:\\; "; OdbcConnection myconn = new OdbcConnection(connect); string sqlt ="CREATE TABLE aa.DBF (cc int(10))"; myconn.Open(); OdbcCommand olec = new OdbcCommand(sqlt, myconn); try { int i = olec.ExecuteNonQuery(); MessageBox.Show("'" + i + "'success"); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { olec.Dispose(); myconn.Close(); } //string ole_connstring = @"Provider=microsoft.jet.oledb.5.0;Data Source=D:\;"; //System.Data.OleDb.OleDbConnection ole_conn = new System.Data.OleDb.OleDbConnection(ole_connstring); //try //{ // ole_conn.Open(); // System.Data.OleDb.OleDbCommand cmd1 = new System.Data.OleDb.OleDbCommand // ("Create Table TestTable (Field1 int, Field2 char(10),Field float(10,2))", // ole_conn); // System.Data.OleDb.OleDbCommand cmd2 = new System.Data.OleDb.OleDbCommand // ("Insert Into TestTable values (1,'Hello3',520.20)", ole_conn); // System.Data.OleDb.OleDbCommand cmd3 = new System.Data.OleDb.OleDbCommand // ("Insert Into TestTable values (2,'Hello4',18076.60)", ole_conn); // cmd1.ExecuteNonQuery(); // cmd2.ExecuteNonQuery(); // cmd3.ExecuteNonQuery(); //} //catch (Exception ex) //{ // MessageBox.Show(ex.Message); //} //finally //{ // ole_conn.Close(); //} } //导入数据 private void btnIn_Click(object sender, EventArgs e) { } private void Form1_Load(object sender, EventArgs e) { Bind(); } private void Bind() { try { conn = new System.Data.Odbc.OdbcConnection(); string table = @"C:\测试例子\Dbf\prepur.dbf"; string connStr = @"Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=" + table + ";Exclusive=No;NULL=NO;Collate=Machine;BACKGROUNDFETCH=NO;DELETED=NO"; conn.ConnectionString = connStr; conn.Open(); string sql = @"select * from " + table; OdbcDataAdapter da = new OdbcDataAdapter(sql, conn); DataTable dt = new DataTable(); da.Fill(dt); this.dataGridView1.DataSource = dt.DefaultView; //MessageBox.Show(dt.Rows[0][0].ToString()); } catch { } finally { conn.Close(); } } private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { try { conn = new System.Data.Odbc.OdbcConnection(); string table = @"C:\测试例子\Dbf\table1.dbf"; string connStr = @"Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=" + table + ";Exclusive=No;NULL=NO;Collate=Machine;BACKGROUNDFETCH=NO;DELETED=NO"; conn.ConnectionString = connStr; conn.Open(); string id = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString(); string sql = @"select * from " + table + " where id='" + id + "'"; OdbcDataAdapter da = new OdbcDataAdapter(sql, conn); DataTable dt = new DataTable(); da.Fill(dt); txtId.Text = id; txtName.Text = dt.Rows[0]["name"].ToString(); txtAddress.Text = dt.Rows[0]["address"].ToString(); } catch { } finally { conn.Close(); } } private void Add() { conn = new System.Data.Odbc.OdbcConnection(); string table = @"C:\temp\Dbf\table1.dbf"; string connStr = @"Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=" + table + ";Exclusive=No;NULL=NO;Collate=Machine;BACKGROUNDFETCH=NO;DELETED=NO"; conn.ConnectionString = connStr; conn.Open(); OdbcCommand cmd = new OdbcCommand(); cmd.Connection = conn; string sql = "insert into " + table + " values('" + txtId.Text + "','" + txtName.Text + "','" + txtAddress.Text + "')"; cmd.CommandText = sql; cmd.CommandType = CommandType.Text; cmd.ExecuteNonQuery(); Bind(); } private void btnTOSQL_Click(object sender, EventArgs e) { try { string sql = "Insert Into dbftosql select * From openrowset('MSDASQL','Driver=Microsoft Visual FoxPro Driver;SourceType=DBF;SourceDB=C:\\temp\\Dbf','select * from table1.dbf')"; SqlConnection con = new SqlConnection("server=.;database=labelprint;uid=sa;pwd=sa"); con.Open(); SqlCommand cmd = new SqlCommand(sql, con); cmd.ExecuteNonQuery(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } }
功能介绍:增;减;修;查;用户权限 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 MRCY { public partial class frmUser : Form { public frmUser() { InitializeComponent(); } private void BindData() { SqlConnection conn = BaseClass.DBConn.CyCon(); SqlDataAdapter sda = new SqlDataAdapter("select WaiterName,CardNum,WaiterNum,Sex,Age,Tel,ID from tb_Waiter order by ID desc", conn); DataSet ds = new DataSet(); sda.Fill(ds); dataGridView1.DataSource = ds.Tables[0]; } private void button7_Click(object sender, EventArgs e) { this.Close(); BindData(); } private void frmUser_Load(object sender, EventArgs e) { comboBox1.SelectedIndex = 0; } private void button5_Click(object sender, EventArgs e) { BindData(); } private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { txtname.Text = dataGridView1.SelectedCells[0].Value.ToString(); txtjc.Text = dataGridView1.SelectedCells[1].Value.ToString(); txtbjf.Text = dataGridView1.SelectedCells[2].Value.ToString(); comboBox1.SelectedItem = dataGridView1.SelectedCells[3].Value.ToString().Trim(); txtlx.Text = dataGridView1.SelectedCells[4].Value.ToString(); txtbz.Text = dataGridView1.SelectedCells[5].Value.ToString(); button2.Enabled = true; button6.Enabled = true; } private void button1_Click(object sender, EventArgs e) { txtname.Text = ""; txtlx.Text = ""; txtjc.Text = ""; txtbz.Text = ""; txtbjf.Text = ""; txtname.Enabled = true; txtjc.Enabled = true; txtbjf.Enabled = true; comboBox1.Enabled = true; txtlx.Enabled = true; txtbz.Enabled = true; button3.Enabled = true; button4.Enabled = true; button2.Enabled = false; } private void button2_Click(object sender, EventArgs e) { button1.Enabled = false; button3.Enabled = true; button4.Enabled = true; txtname.Enabled = false; txtjc.Enabled = true; txtbjf.Enabled = true; this.comboBox1.Enabled = true; txtlx.Enabled = true; txtbz.Enabled = true; } private void button3_Click(object sender, EventArgs e) { SqlConnection conn = BaseClass.DBConn.CyCon(); conn.Open(); SqlCommand cmd = new SqlCommand("select count(*) from tb_Waiter where WaiterName='" + txtname.Text + "'", conn); int i = Convert.ToInt32(cmd.ExecuteScalar()); if (i > 0) { cmd = new SqlCommand("update tb_Waiter set WaiterName='" + txtname.Text + "',CardNum='" + txtjc.Text + "',WaiterNum='" + txtbjf.Text + "',Sex='" + comboBox1.SelectedItem.ToString() + "',Age='" + txtlx.Text + "',Tel='" + txtbz.Text + "' where ID='" + dataGridView1.SelectedCells[6].Value.ToString() + "'", conn); cmd.ExecuteNonQuery(); conn.Close(); BindData(); button1.Enabled = true; button2.Enabled = false; button3.Enabled = false; button4.Enabled = false; button5.Enabled = true; button6.Enabled = false; button7.Enabled = true; txtname.Enabled = false; } else { cmd = new SqlCommand("insert into tb_Waiter(WaiterName,CardNum,WaiterNum,Sex,Age,Tel) values('" + txtname.Text + "','" + txtjc.Text + "','" + txtbjf.Text + "','" + comboBox1.SelectedItem.ToString() + "','" + txtlx.Text + "','" + txtbz.Text + "')", conn); cmd.ExecuteNonQuery(); conn.Close(); BindData(); button1.Enabled = true; button2.Enabled = false; button3.Enabled = false; button4.Enabled = false; button5.Enabled = true; button6.Enabled = false; button7.Enabled = true; txtname.Enabled = false; } } private void button4_Click(object sender, EventArgs e) { button1.Enabled = true; button2.Enabled = false; button3.Enabled = false; button4.Enabled = false; button6.Enabled = false; txtname.Enabled = false; txtjc.Enabled = false; txtbjf.Enabled = false; this.comboBox1.Enabled = false; txtlx.Enabled = false; txtbz.Enabled = false; } private void button6_Click(object sender, EventArgs e) { SqlConnection conn = BaseClass.DBConn.CyCon(); conn.Open(); SqlCommand cmd = new SqlCommand("delete from tb_Waiter where ID='" + dataGridView1.SelectedCells[6].Value.ToString() + "'", conn); cmd.ExecuteNonQuery(); conn.Close(); BindData(); } } }

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧