datagridview无数据显示问题处理

1.if (dataGridView1.Rows.Count < 1) return;
 int index = dataGridView1.CurrentRow.Index;     //获取当前选中行索引号
dataGridView1.Rows[index].Selected = true;      //加亮显示
string Inven_No = this.dataGridView1["InventoryNo", index].Value.ToString();
2.if (ds.Tables["tb"].Rows.Count == 0)
   {  MessageBox.Show("没有符合条件的数据"); }
3.
    OracleDataAdapter da = new OracleDataAdapter(strSQL, conn);
    ds = new DataSet();
    da.Fill(ds);
     if (ds.Tables[0].Rows.Count == 0)
     {
        MessageBox.Show("没有符合条件的数据");
      }
    da.Dispose();
    this.bindingSource1.DataSource = ds.Tables[0];
    this.bindingNavigator1.BindingSource = bindingSource1;
    this.dataGridView1.DataSource = bindingSource1;
    this.dataGridView1.DefaultCellStyle.SelectionBackColor = Color.LightSteelBlue;
    this.dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Black;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
下面是一个简单的示例,演示如何使用C#从数据库中检索数据并在Windows Forms应用程序中使用DataGridView控件显示它们: 1.创建数据库表和数据:首先,您需要在MySQL、MSSQL或其他数据库管理系统中创建一个表并添加一些数据。下面是一个示例表Restaurant,其中包含ID(int)、Name(varchar)和Address(varchar)列: CREATE TABLE Restaurants ( ID int NOT NULL AUTO_INCREMENT, Name varchar(255) DEFAULT NULL, Address varchar(255) DEFAULT NULL, PRIMARY KEY (ID) ); INSERT INTO Restaurants (Name, Address) VALUES ('McDonalds', '123 Main St'), ('Burger King', '456 1st Ave'), ('Pizza Hut', '789 2nd St'), ('KFC', '111 Elm St'); 2.添加DataGridView控件:在您的Windows Forms应用程序中,添加一个DataGridView控件,并将其命名为dataGridView1。 3.将数据绑定到DataGridView:在窗体的Load事件处理程序中,添加以下代码: private void Form1_Load(object sender, EventArgs e) { // 连接到数据库 string connectionString = "server=localhost;user=root;password=123456;database=test_db"; MySqlConnection connection = new MySqlConnection(connectionString); connection.Open(); // 查询数据 string query = "SELECT * FROM Restaurants"; MySqlCommand command = new MySqlCommand(query, connection); MySqlDataAdapter adapter = new MySqlDataAdapter(command); DataTable data = new DataTable(); adapter.Fill(data); // 绑定数据到DataGridView dataGridView1.DataSource = data; } 请注意,上面的代码使用MySQL数据库,因此您需要安装MySQL.Data NuGet包。如果使用MSSQL等其他数据库管理系统,则需要使用其他数据提供程序。 4.运行应用程序:现在,运行应用程序,您应该能够看到从数据库中检索的数据在DataGridView控件中显示出来。 这只是一个简单的示例,您可以根据自己的需求进行更改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

皮特大熊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值