在DataGrid视图中显示框架的数据加载 [英] Show Data OnLoad of Frame in DataGrid View

查看:72
本文介绍了在DataGrid视图中显示框架的数据加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上,我希望从DataGridView(窗体的OnLoad)中的数据库中获取数据.我已经编写了这段代码,但这无法正常工作.我检查了连接建立的正确性,并且查询也很麻烦,但是问题出在哪里?

Actually i want data from database in DataGridView, OnLoad of Form. I have write this code but this is not working. I have checked that connection established correclty and query is also wrtie but where is problem?

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.Sql;
using System.Data.SqlClient;

namespace DISM_Project1
{
    public partial class AvalForm : Form
    {
        private SqlConnection xSqlConnection;
        private SqlCommand xSqlCommand;
        //private SqlDataAdapter xSqlDataAdapter;

        public AvalForm()
        {
            InitializeComponent();
        }

        private void AvalForm_Load(object sender, EventArgs e)
        {
            try
            {
                //Making Connection
                xSqlConnection = new SqlConnection("Data Source=WaQaS-PC;Initial Catalog=BookingSystem;User ID=sa;Password=123");
                xSqlConnection.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show("" + ex);
            }
            xSqlCommand = new SqlCommand("Select Pno, PStatus from Unit1_Table where PStatus = ''Availible'' UNION Select Pno, PStatus from Unit2_Table where PStatus = ''Availible''", xSqlConnection);
            //SqlDataReader xSqlDataReader = null;
            //xSqlDataReader = xSqlCommand.ExecuteReader();
            //var objDataGridView = new DataGridView();
            dataGridView1.DataSource = xSqlCommand;

        }
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //dataGridView1.DataSource = xSqlCommand;
        }
    }
}

推荐答案

写道:​​

PStatus =可用""

PStatus = ''Availible''"



哇-您的数据库架构多么糟糕.您的数据库中是否有可能正确拼写了"available"?无论哪种方式,您的身份都应该映射到一个数字,这样拼写错误就不会破坏它.

您是否不需要在GridView上调用DataBind()?



Wow - what a horrible database schema you have. Is it possible that ''available'' is spelled correctly in your DB ? Either way, your status should map to a number so that a spelling mistake can''t break it.

Do you not need to call DataBind() on your GridView ?


我检查了拼写错误,数据库中的Speeling of Available相同,并且是可用".

如何在GridView上调用DataBind()?
I have checked spelling Mistake, Speeling of Available is same in database and that is "Availible".

How to call DataBind() on GridView?


这篇关于在DataGrid视图中显示框架的数据加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆