世俱杯 2025

如何用Aspose.Cells实现数据库和工作簿的绑定

原创|其它|编辑:郝浩|2012-09-11 15:20:37.000|阅读 418 次

概述:Aspose.Cells要将数据库绑定到一个工作表,应遵循的步骤为:访问将被绑定的表……

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

Aspose.Cells要将数据库绑定到一个工作表,应遵循以下步骤:

  • 访问将被绑定的表
  • 指定一个数据源(可以是一个DataSet,DataTable或DataView等)的工作表
  • 创建列绑定到数据源
  • 创建验证。在我们的实例中,我们将为ProductID列创建一个必要的验证
  • 指定数字格式设置。在我们的实例中,我们将产品价格列的数字格式设置为Currency3
  • 加载和填充数据集
  • 实现绑定

实例代码:

[C#]

 //Implementing Page_Load event handler
private void Page_Load(object sender, System.EventArgs e)
{
    //Checking if there is not any PostBack
    if (!IsPostBack)
    {
        //Accessing a desired worksheet
         WebWorksheet sheet = GridWeb1.WebWorksheets[0];

        //Specifying Data Source for the worksheet
         sheet.DataSource = dataSet11;

        //Specifying Products tables as the DataMember
         sheet.DataMember = "Products";

        //Creating data bound columns automatically
         sheet.CreateAutoGenratedColumns();

        //Creating REQUIRED validation for ProductID column
        Validation v = new Validation();
        v.IsRequired = true;
         sheet.BindColumns[0].Validation = v;

        //Setting Number Format of ProductPrice column to Currency3
         sheet.BindColumns[2].NumberType = NumberType.Currency3;

        try
        {
            //Filling DataSet
            oleDbDataAdapter1.Fill(dataSet11);
        }
        finally
        {
            //Closing database connection
            oleDbConnection1.Close();
        }

        //Binding worksheet with DataSet
        sheet.DataBind();
    }
}
 

 

[VB.NET]

 'Implementing Page_Load event handler
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'Checking if there is not any PostBack
    If Not IsPostBack Then
        'Accessing a desired worksheet
         Dim sheet As WebWorksheet = GridWeb1.WebWorksheets(0)

        'Specifying Data Source for the worksheet
         sheet.DataSource = dataSet11

        'Specifying Products tables as the DataMember
         sheet.DataMember = "Products"

        'Creating data bound columns automatically
         sheet.CreateAutoGenratedColumns()

        'Creating REQUIRED validation for ProductID column
        Dim v As Validation = New Validation()
        v.IsRequired = True
         sheet.BindColumns(0).Validation = v

        'Setting Number Format of ProductPrice column to Currency3
         sheet.BindColumns(2).NumberType = NumberType.Currency3

        Try
            'Filling DataSet
            oleDbDataAdapter1.Fill(dataSet11)
        Finally
            'Closing database connection
            oleDbConnection1.Close()
         End Try

        'Binding worksheet with DataSet
        sheet.DataBind()
    End If
End Sub
 
最后,编译并运行此Web应用程序的。 Web窗体加载后,你会看到从数据库加载到工作表中的数据:

 


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@dpuzeg.cn

文章转载自:慧都控件网

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP