我如何将从存储过程创建的绑定源绑定到 gridview

How do i bind a bindingsource created from stored procedure to a gridview

首先,我的数据库中有 3 个表:

Student
StudentID
Name
Adress
OtherInfo
TuitionFee
StudentID
Years
Semester
FeeAmount
Detailed_TuitionFee
StudentID
Years
Semester
PaymentDate
PaymentAmount

然后我创建了一个存储过程 select [TuitionFee ][Detailed_TuitionFee ] 上的某些列,使用 StudentID 作为参数。

create procedure 
    @StudentID
as 
begin
    select 
    from [TuitionFee ]
    left outer join [Detailed_TuitionFee ] on TuitionFee.StudentID = Detailed_TuitionFee.StudentID 
                                           and TuitionFee.Semester = Detailed_TuitionFee.Semester
    where TuitionFee.StudentID = @StudentID 
      and Detailed_TuitionFee.StudentID = @StudentID

在C#中,我写的代码是

BindingSource bdsTuitionInfo = new BindingSource();
String studentID; 
GridControl gCtrlTuition;
GridView gViewTuition;
String sqlCmd = "exec Stored procedure" + studentID;

DataTable dt = Program.ExecSqlDataTable(sqlCmd); // create a Data table from SP
bdsTuitionInfo.DataSource = dt; // import data table into Binding source
gCtrlTuition.DataSource = bdsTuitionInfo; // import Binding source into Grid Control
gViewTuition.DataSource = bdsTuitionInfo; // The code shows ERROR: "Property or indexer 'property' cannot be assigned to -- it is read only" here

如何在 GridView 中显示此 BindingSource 的数据? 在我的 GridView?

中有什么需要调整或配置的吗?

首先将你的数据填入数据表dt。然后只需在您的页面上添加 GridView 控件。然后使用(如果您使用 ASP.NET Web 表单):

GridView.DataSource = dt;
//GridView.Databind(); //just in asp.net webform

ASP.NET Web 窗体中有一个网格视图控件,我想您正在使用它。

我从未使用过 DevExpress 的网格,但它们的 official example 似乎表明您不需要在 GridView 上分配数据源;您将源分配给 GridControl,然后可以通过 GridControl 访问 GridView 以设置其他属性..

        gridControl1.DataSource = DataHelper.GetData(30);
        // The grid automatically creates columns for the public fields found in the data source. 
        // Calling the gridView1.PopulateColumns method is not required unless the gridView1.OptionsBehavior.AutoPopulateColumns is disabled

        // The grid automatically creates a GridView that presents the underlying data as a two-dimensional table.
        GridView gridView1 = gridControl1.MainView as GridView;

        GridView;

        // Obtain created columns.
        GridColumn colCompany = gridView1.Columns["CompanyName"];
        ...