来自sql数据库的记录未显示在datagrid上

时间:2014-03-28 20:16:05

标签: c# sql datagrid

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

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

1 个答案:

答案 0 :(得分:1)

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

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

相关问题