Gridcontrol Master and slave table settings

Source: Internet
Author: User
Tags datetime

Gridcontrol Master and slave table settings There are currently 2 ways I know one is to use DataRelation, which has been to add childs to the list of objects. To tell you the truth, the master-slave watch got a big head at first. Because it's a project, it's all about using objects. And the return is IList. Master and slave table relationships have been a few days, and now finally solved, but also know how to get the details of the data.

You first use the relationships in the object to set the primary table relationship.

The code is as follows: Set the master-slave table, get the Main table row, click the Schedule row to get the schedule row.

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 DevExpress.XtraGrid.Views.BandedGrid;
Using DevExpress.XtraGrid.Views.Base;
Using System.Linq;
Using DevExpress.XtraGrid.Columns;
Using DevExpress.XtraEditors.Repository;
Using DevExpress.XtraGrid.Views.Grid;

Namespace test
{
public partial class Form3:form
{

GridView Gridviewdetail = new GridView ();//Because the schedule is visible only at run time, we need to set a schedule to get the schedule data.

Public Form3 ()
{
InitializeComponent ();//Add a Girdcontrol to the interface.

Initgrid ();
}
private void Initgrid ()
{
Simulate several data
list<classno> ListDataSource2 = new list<classno> ();
Listdatasource2.add (New Classno (1, 1, "Computer class 01", "Remarks 01"));
Listdatasource2.add (New Classno (2, 2, "Computer class 02", "Remarks 02"));
Listdatasource2.add (New Classno (3, 3, "Computer class 03", "Remarks 03"));
Listdatasource2.add (New Classno (4, 1, "Computer class", "Remarks 01"));


list<record> Listdatasource = new list<record> ();
Listdatasource.add (New Record (1, "Zhang San", "Male", Convert.todatetime ("1989-5-6"), 115.5f, 101, (+), "", ListDataSource2));

There are classno in the record, so the run-time details are visible. Must be list, if it is IList can not appear in detail. , if it is IList, you can convert IList to list.

Listdatasource.add (New Record (2, "John Doe", "female", Convert.todatetime ("1987-12-23"), the "", "", "ListDataSource2");
Listdatasource.add (New Record (3, "Harry", "Female", Convert.todatetime ("1990-2-11"), N, 41.5f, "", ListDataSource2));
Listdatasource.add (New Record (4, "Zhao Liu", "Male", Convert.todatetime ("1988-9-1"), 119, 108, 110, "Remark line", ListDataSource2));
Binds the data source and displays
Gridcontrol1.datasource = Listdatasource;
}


private void Gridview1_click (object sender, EventArgs e)//Get Row data
{
int numnber = Gridview1.focusedrowhandle;
Object id = gridview1.getrowcellvalue (numnber, "id");//Gets the ID of the record.
}

private void Gridview1_masterrowexpanded (object sender, DevExpress.XtraGrid.Views.Grid.CustomMasterRowEventArgs e) ( This event occurs after a detail is opened)
{
Gridviewdetail= Gridview1.getdetailview (E.rowhandle, E.relationindex) as devexpress.xtragrid.views.grid.gridview;/ /Assign detail data to Gridviewdetail

Gridviewdetail. Optionsbehavior.editable = False;//gridviewdetail Not editable that is obviously not editable

Gridviewdetail.  Click + = new System.EventHandler (This.gridview_click); Sets the detail Click event to occur when the detail is clicked. We use it to get detailed data.

}

private void Gridview_click (object sender, EventArgs e)
{
int numnber = Gridviewdetail. Focusedrowhandle;
Object id = gridviewdetail. Getrowcellvalue (Numnber, "id");//Gets the ID in the detail.
}

#region Run-time binding to the data source record that implements the IList interface


public class Record
{
int id;
DateTime birth;
String name, sex, remark;
Float math, Chinese, 中文版;
List<classno> Classnos;
public Record (int ID, string name, string sex, DateTime birth, float math, float Chinese, float 中文版, string remark, Li St<classno> Classnos)
{
This.id = ID;
THIS.name = name;
This.sex = sex;
This.birth = birth;
This.math = Math;
This.chinese = Chinese;
This.english = 中文版;
This.remark = remark;
This.classnos = Classnos;
}
public int ID {get {return ID;}}
public string Name
{
get {return name;}
set {name = value;}
}
public string Sex
{
get {return sex;}
set {sex = value;}
}
Public DateTime Birth
{
get {return birth;}
set {birth = value;}
}
public float Math
{
get {return math;}
set {math = value;}
}
Public float Chinese
{
get {return chinese;}
set {Chinese = value;}
}
public float 中文版
{
get {return 中文版;}
set {中文版 = value;}
}
public string Remark
{
get {return remark;}
set {remark = value;}
}
Public list<classno> Getclassnos
{
get {return classnos;}
set {Classnos = value;}
}


}

#endregion
#region Runtime is bound to the data source Classno that implements the IList interface,


public class Classno
{
int id,recordid;

string remark;

public Classno (int id, int recordId, string calssno, string remark)
{
This.id = ID;
This.recordid =recordid;
This.remark = remark;
}
public int ID {get {return ID;}}
public int RecordId
{
get {return recordId;}
set {recordId = value;}
}

public string Remark
{
get {return remark;}
set {remark = value;}
}
}

#endregion


}
}

There is also a way to set up master-slave table relationships. Using Datset

     

Note: Gridcontrol contains the GridView add GridControl1

To connect to a database:

String souce = "server= (local); Integrated SECURITY=SSPI;DATABASE=DX";

SqlConnection Conn =new SqlConnection (souce);

Conn. Open ();

String str = "SELECT * from Base_color";

String str2 = "SELECT * from Base_detail";

String STR3 = "SELECT * from Base_three";

SqlDataAdapter da =new SqlDataAdapter (str, con);

SqlDataAdapter da2 =new SqlDataAdapter (str2, con);

SqlDataAdapter Da3 =new SqlDataAdapter (Str3, con);

DataSet DS =new DataSet ();

A.fill (ds, "color");

Da2. Fill (ds, "detail");

Da3. Fill (ds, "three");

DataRelation dr = New DataRelation ("2 Layer", DS. tables["Color"]. columns["id"],ds. tables["Detail"]. columns["Color_no"]);

Set the relationship, Ds.color (table). Id (field) = Ds.detail. Color_no

DataRelation DR2 = new DataRelation ("3 Layer", DS. tables["Color"]. columns["id"],ds. Tables["three"]. columns["Color_no"]);

DataRelation DR3 = new DataRelation ("4 Layer", DS. tables["Detail"]. columns["id"],ds. Tables["three"]. columns["Color_no"]);

Ds. Relations.Add (DR);

Ds. Relations.Add (DR2);

Ds. Relations.Add (DR3);

Gridcontrol1.datasource = ds. tables[0];//Setting the Main table

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.