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)
{

}
}
}

...全文
524 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
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
功能介绍:增;减;修;查;用户权限 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(); } } }

111,098

社区成员

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

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

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