【问题标题】:Records from sql database are not displaying on datagrid来自 sql 数据库的记录未显示在数据网格上
【发布时间】:2014-03-28 20:16:05
【问题描述】:

所以我在显示我的数据库中的记录时遇到了问题。我的表单只有两个数据网格。这是我的代码:

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 testare_selectie
{
    public partial class Form1 : Form
    {
        DataSet ds = new DataSet();
        SqlDataAdapter daVendors = new SqlDataAdapter();
        SqlDataAdapter daInvoices = new SqlDataAdapter();
        SqlConnection cs = new SqlConnection("Data Source=USER-PC\\SQLEXPRESS;Initial Catalog=db;integrated security = TRUE");

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SqlCommand slctVendors = new SqlCommand("SELECT * FROM Developerss ",cs);
            daVendors.SelectCommand = slctVendors;
            daVendors.Fill(ds,"Developerss");

            SqlCommand slctInvoices = new SqlCommand("SELECT * FROM Games", cs);
            daVendors.SelectCommand = slctInvoices;
            daVendors.Fill(ds, "Games");

            dgV.DataSource = ds.Tables["Developerss"];
            dgI.DataSource = ds.Tables["Games"];
        }
    }
}

另外我想提一下,在此之前我之前做过一个项目,它也有一个显示按钮并且它有效,我不确定为什么这个不是。我跟进了来自 youtube 的教程。我检查了很多次拼写错误或连接参数,我似乎无法弄清楚这一点。

提前谢谢你!

视频来源:https://www.youtube.com/watch?v=m_K__V0rIz4

【问题讨论】:

  • 缺少您的 dgV.DataBind() 和 dgI.DataBind()?
  • 那家伙没有添加它 :( 。另外,当我显示数据时,我没有将它添加到我以前的项目中

标签: c# sql datagrid


【解决方案1】:

WinForms DataGridView 不需要数据绑定。所以那部分是正确的。如果数据未显示,请检查以下内容:

  1. 数据存在于源表中。 (直接查询或 SQL 分析器结果以获得真实指示)
  2. 检查数据网格视图的可见性。
  3. ds.Tables[0].Rows.Count 上的断点
  4. 检查Form1_Load 方法是否真正被调用。检查表单的属性以查看 Load 事件连接。

这应该指向真正的原因。

【讨论】:

  • Form1_Load 是否已连接?也许根本没有调用事件处理程序。尝试双击表单的标题栏,看看它是否创建了新的事件处理程序或进入相同的方法。
  • 是的,这似乎解决了它!非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-24
  • 1970-01-01
  • 1970-01-01
  • 2022-01-09
相关资源
最近更新 更多