如何在ado.net中绑定datagridview [英] how to bind datagridview in ado.net

查看:62
本文介绍了如何在ado.net中绑定datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想要使用ado.net在表单加载上绑定datagridview无法创建字段Physio_cureTable的子列表。错误iam粘贴在这里我的代码请给我建议或帮助



want to bind datagridview on form load using ado.net "Child list for field Physio_cureTable cannot be created". error iam paste here my code please give me suggestions or help

con = new SqlConnection("Data Source=ADMIN\SQLEXPRESS;Initial Catalog=PhysioCure; Integrated Security=true");
        sda = new SqlDataAdapter("select RegisterNo,RegistrationDate,Stimulation,PationName,DateOfBirth,ContactNo,Occupation,Age,Sex,Weight,Chief_Complain,Investigation_Result,PastHistoryAny,Physical_Examination,Ref_By_Doctor,Medications,Prognosis,Electro_Therapy,Neuro_Rehabilitation,Ortho_Rehabilitation,Cardio_Pulmonery_Rehabilitation,Sports_Rehabilitation from Physio_cureTable where Syncoperation <>'D'",con);
        ds = new DataSet();
        sda.Fill(ds);
        dataGridView1.AutoGenerateColumns = true;
        dataGridView1.DataSource = ds;

        dataGridView1.DataMember = "Physio_cureTable".ToString();// error coming this line 

推荐答案

问题解决朋友

这里是解决方案创建一个方法n传递该方法表单加载

problem solve friends
here is the solution create one method n pass that method form load
private void GetData(string selectCommand)
        {
            try
            {
                // Specify a connection string. Replace the given value with a  
                // valid connection string for a Northwind SQL Server sample 
                // database accessible to your system.
                String connectionString =
                    "Integrated Security=true;" +
                    "Initial Catalog=PhysioCure;Data Source=ADMIN\\SQLEXPRESS";

                // Create a new data adapter based on the specified query.
                sda = new SqlDataAdapter("select t.RegisterNo,RegistrationDate,Stimulation,PationName,DateOfBirth,ContactNo,Occupation,Age,Sex,Weight,Chief_Complain,Investigation_Result,PastHistoryAny,Physical_Examination,Ref_By_Doctor,Medications,Prognosis,Electro_Therapy,Neuro_Rehabilitation,Ortho_Rehabilitation,Cardio_Pulmonery_Rehabilitation,Sports_Rehabilitation from Physio_cureTable t ", connectionString);

                // Create a command builder to generate SQL update, insert, and 
                // delete commands based on selectCommand. These are used to 
                // update the database.
                SqlCommandBuilder commandBuilder = new SqlCommandBuilder(sda);

                // Populate a new data table and bind it to the BindingSource.
                DataTable table = new DataTable();
                table.Locale = System.Globalization.CultureInfo.InvariantCulture;
                sda.Fill(table);
                dataGridView1.DataSource = table;

                // Resize the DataGridView columns to fit the newly loaded content.
                dataGridView1.AutoResizeColumns(
                    DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
            }
            catch (SqlException)
            {
                MessageBox.Show("To run this example, replace the value of the " +
                    "connectionString variable with a connection string that is " +
                    "valid for your system.");
            }
        }



并在表单上传递此方法




and pass this method on form load

private void SearchPage_Load(object sender, EventArgs e)
       {

           GetData("select t.RegisterNo,RegistrationDate,Stimulation,PationName,DateOfBirth,ContactNo,Occupation,Age,Sex,Weight,Chief_Complain,Investigation_Result,PastHistoryAny,Physical_Examination,Ref_By_Doctor,Medications,Prognosis,Electro_Therapy,Neuro_Rehabilitation,Ortho_Rehabilitation,Cardio_Pulmonery_Rehabilitation,Sports_Rehabilitation from Physio_cureTable t ");

       }


这篇关于如何在ado.net中绑定datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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