窗口表单-使用gridview的数据绑定概念 [英] Window forms -- Data binding concepts using gridview

查看:77
本文介绍了窗口表单-使用gridview的数据绑定概念的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要更好的解决方案来处理数据绑定中的关系..谢谢您



在组合框中输入部门名称时,在这里我必须在两个gridviews中显示两个表及其详细信息..

但是在这里添加关系并将数据集插入到gridview时是可以的,但是当我为网格视图插入dataMember时,它表明无法为数据集创建子字段.

dataGridView1.DataSource = dtviewMang;

dataGridView1.DataMember ="EmpDs.relEmpDetails";

在这里,我采用了简单的Sql Table-员工为Emp,部门为Dept,部门详细信息为deptDetails.

请解决问题,如果您得到了答案,请将其粘贴到此处.

谢谢..











i want better soloution to work with relations in data binding..thank u



Here i have to display the two tables in two gridviews and their details when i enter department name in combo box..

but here while adding relation and inserting dataset to gridview it''s ok but when i insert dataMember for grid view it show child field can''t be created for the dataset.

dataGridView1.DataSource = dtviewMang;

dataGridView1.DataMember = "EmpDs.relEmpDetails";

here i have taken simple Sql Table -- employee as Emp and Department as Dept and DepartmentDetails as deptDetails.

please solve the problem and if u got the answer please paste it down here..

Thank You..











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


namespace WinForm1
{
    public partial class SampleProg2 : Form
    {
        private String ConnectionString;
        private DataViewManager dtviewMang;
        private DataSet ds;

        public SampleProg2()
        {
            InitializeComponent();

            ConnectionString = "Data Source=ETG-75;Initial Catalog=test;Integrated Security=True";
            SqlConnection con = new SqlConnection(ConnectionString);

            ds = new DataSet("EmpDs");
             

            SqlDataAdapter da1 = new SqlDataAdapter("select * from Emp", con);
            da1.TableMappings.Add("Table", "Emp");
            da1.Fill(ds);

            SqlDataAdapter da2 = new SqlDataAdapter("select * from Dept", con);
            da2.TableMappings.Add("Table", "Dept");
            da2.Fill(ds);


            SqlDataAdapter da3 = new SqlDataAdapter("select * from DeptDetails", con);
            da3.TableMappings.Add("Table", "DeptDetail");
            da3.Fill(ds);
            
         
            //show created table names with in the dataset
            string myMessage = "Table Mappings";
            for (int i = 0; i < ds.Tables.Count; i++)
            {
                myMessage += i.ToString() + " " + ds.Tables[i].ToString()+" ";
            
            }
             
            //Establish the relation between Employee  and dept tables

            System.Data.DataRelation relEmpDetails;
            //  System.Data.DataColumn colemp1;
            System.Data.DataColumn colDept1;
            System.Data.DataColumn colemp1 = ds.Tables[0].Columns["Deptno"];
            colDept1 = ds.Tables[1].Columns["Deptno"];
            relEmpDetails = new System.Data.DataRelation("RelEmpDetails", colDept1, colemp1 );
            ds.Relations.Add(relEmpDetails);


            System.Data.DataRelation relDetpDetails;
            System.Data.DataColumn colDept2;
            System.Data.DataColumn colDeptdetail2;
           colDept2 = ds.Tables[1].Columns["Deptno"];
            colDeptdetail2 = ds.Tables[2].Columns["deptno"];
            relDetpDetails = new System.Data.DataRelation("RelDeptDetails", colDept2, colDeptdetail2  );
            ds.Relations.Add(relDetpDetails);

            //show created relations with in the dataset

            myMessage += "Relations Mappings:";
            for (int i = 0; i < ds.Relations.Count; i++)
            {
                myMessage += i.ToString() + " " + ds.Relations[i].ToString() + " ";

            
            }
            textBox1.Text = myMessage;

            // The DataViewManager returned by the DefaultViewManager
            // property allows you to create custom settings for each
            // DataTable in the DataSet.
            dtviewMang = ds.DefaultViewManager;


错误出现在这里:


Error shows up here:

dataGridView1.DataSource = dtviewMang;

dataGridView1.DataMember = "EmpDs.relEmpDetails";//Error Here

dataGridView2.DataSource = dtviewMang;

dataGridView2.DataMember = "EmpDs.RelEmpDetails.RelDeptDetails"; // Error here

comboBox1.DataSource = dtviewMang;
comboBox1.DisplayMember = "EmpDs.DName";
comboBox1.ValueMember = "EmpDs.Dno"; //error here


            textBox2.DataBindings.Add("Text", dtviewMang, "EmpDs.Loc");
            textBox3.DataBindings.Add("Text", dtviewMang, "EmpDs.Comments");


        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.BindingContext[dtviewMang, "EmpDs"].Position > 0)
            {
                this.BindingContext[dtviewMang, "EmpDs"].Position--;
            }


        }

        private void button2_Click(object sender, EventArgs e)
        {
            CurrencyManager cm = (CurrencyManager)this.BindingContext[dtviewMang, "EmpDs"];
            if (cm.Position < cm.Count - 1)
            {
                cm.Position++;
            }

        }

       
    }
}



[edit]已添加代码块-OriginalGriff [/edit]



[edit]Code block added - OriginalGriff[/edit]

推荐答案

亲爱的同伴,

为什么不尝试linq.看一下这篇文章.我希望它能做到.这基本上与Entity Framework有关,但本文中存在对您问题的答案.

实体框架简介 [
Dear fellow,

Why not you try linq. Take a look at this article. I hope it will do. This basically relates to Entity framework but the answer to your question exists in this article.

Introduction to the Entity Framework[^]


//假设Fill_Details_By_Dept();是一个函数,返回一个
包含2个表的DataSet ds,
1 ds.tables [0]代表您的要求2列
2.ds.tables [1]代表您的要求1列

@假设您的网格有3列,并且网格名称为grd,然后
//suppose Fill_Details_By_Dept(); is a function return a
DataSet ds which contains 2 tables ,
1 ds.tables[0] conatins your req 2 columns
2.ds.tables[1] conatins your req 1 columns

@ suppose your grid hai 3 columns and your grid name is grd then
ds2 = Fill_Details_By_Dept();
                      
      for (int x = 0; x < ds.Tables[2].Rows.Count; x++)
                {
                    grd.Rows[x].Cells[0].Value = ds.Tables[0].Rows[x].ItemArray[0];
                    grd.Rows[x].Cells[0].Value = ds.Tables[0].Rows[x].ItemArray[1];
                    grd.Rows[x].Cells[1].Value = ds.Tables[1].Rows[x].ItemArray[0];



因此您的网格中会填充您想要的结果....


enjoy:)



so your grid populate with the result you want....


enjoy :)


您似乎正在尝试将网格的数据源设置为关系.网格应该被赋予一个表或视图(或其他与数组无关的非System.Data东西,如数组,列表,IEnumerables等).尝试"EmpDs.Emp",或直接将数据源设置为表:

You seem to be trying to set the data source of the grid to a relation. Grids expect to be given a table or view (or other non-System.Data things like arrays, lists, IEnumerables etc that aren''t relevant here). Try ''EmpDs.Emp'', or just set the DataSource to the table directly:

dataGridView1.DataSource = ds.Tables["Emp"];


这篇关于窗口表单-使用gridview的数据绑定概念的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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