Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DataGridView不显示数据_C#_Sql_.net_Winforms_Datagridview - Fatal编程技术网

C# DataGridView不显示数据

C# DataGridView不显示数据,c#,sql,.net,winforms,datagridview,C#,Sql,.net,Winforms,Datagridview,我使用以下代码在表的DataGridView中显示数据。但是DataGridView是空的: namespace WindowsFormsApplication5 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object send

我使用以下代码在表的
DataGridView
中显示数据。但是
DataGridView
是空的:

namespace WindowsFormsApplication5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            MessageBox.Show("sdgds");
            SqlCommand sCommand;
            SqlDataAdapter sAdapter;
            SqlCommandBuilder sBuilder;
            DataSet sDs;
            DataTable sTable;
            string sql = "SELECT * FROM mytable";
            SqlConnection myConnection = new SqlConnection("user id=test;" +
                                       "password=test;server=MOBILE01;" +
                                       "Trusted_Connection=yes;" +
                                       "database=mydatabase; " +
                                       "connection timeout=30");
            myConnection.Open();


            sCommand = new SqlCommand(sql, myConnection);
            sAdapter = new SqlDataAdapter(sCommand);
            sBuilder = new SqlCommandBuilder(sAdapter);
            sDs = new DataSet();

            sAdapter.Fill(sDs);


            sAdapter.Fill(sDs, "mytable");
            dataGridView1.ReadOnly = true;

            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            myConnection.Close();
        }
    }
}

因为您没有设置属性:

dataGridView1.DataSource = sDs.Tables["mytable"];

因为您没有设置属性:

dataGridView1.DataSource = sDs.Tables["mytable"];

您尚未将
数据源
分配到
网格视图

dataGridView1.DataSource = sDs.Tabes[0];

您尚未将
数据源
分配到
网格视图

dataGridView1.DataSource = sDs.Tabes[0];

如果您的目标只是填充DataGridView以显示数据,那么您可能应该去掉Dataset、DataAdapter填充例程:

private void Form1_Load(object sender, EventArgs e)
        {
            MessageBox.Show("sdgds");
            SqlCommand sCommand;
            SqlCommandBuilder sBuilder;
            DataTable sTable;
            string sql = "SELECT * FROM mytable";
            SqlConnection myConnection = new SqlConnection("user id=test;" +
                                       "password=test;server=MOBILE01;" +
                                       "Trusted_Connection=yes;" +
                                       "database=mydatabase; " +
                                       "connection timeout=30");
            myConnection.Open();


            sCommand = new SqlCommand(sql, myConnection);

            /// here is the change - getting data in data reader and filling datatable directly with it.
            var reader = sCommand.ExecuteReader();
            sTable = new DataTable();
            sTable.Load(reader);

            dataGridView1.DataSource = sTable;
            dataGridView1.ReadOnly = true;

            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            myConnection.Close();
        }
    }

如果您的目标只是填充DataGridView以显示数据,那么您可能应该去掉Dataset、DataAdapter填充例程:

private void Form1_Load(object sender, EventArgs e)
        {
            MessageBox.Show("sdgds");
            SqlCommand sCommand;
            SqlCommandBuilder sBuilder;
            DataTable sTable;
            string sql = "SELECT * FROM mytable";
            SqlConnection myConnection = new SqlConnection("user id=test;" +
                                       "password=test;server=MOBILE01;" +
                                       "Trusted_Connection=yes;" +
                                       "database=mydatabase; " +
                                       "connection timeout=30");
            myConnection.Open();


            sCommand = new SqlCommand(sql, myConnection);

            /// here is the change - getting data in data reader and filling datatable directly with it.
            var reader = sCommand.ExecuteReader();
            sTable = new DataTable();
            sTable.Load(reader);

            dataGridView1.DataSource = sTable;
            dataGridView1.ReadOnly = true;

            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            myConnection.Close();
        }
    }

@techno为什么在表单加载时显示消息?需要吗?@techno为什么在表单加载时显示消息?需要吗?