在Silverlight中的datagrid控件中动态添加行和列 [英] Dynamically Add rows and columns in datagrid control in Silverlight

查看:92
本文介绍了在Silverlight中的datagrid控件中动态添加行和列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据网格控件.我想动态添加行和列
以及我想添加复选框列以选择特定的行.
无论做什么,我们都需要在数据库中进行更新....;)

I have a datagrid control. I want to add rows and columns dynamically
as well as I want to add checkbox column to select a particular rows.
And whatever we have done we need to update in the database.... ;)

推荐答案

egdg写道:

我有一个datarid控件.我想动态添加行和列,也想添加复选框列以选择特定的行

I have a datarid control. I want to add rows and columns Dynamically.as well as i want to add checkbox column to select a particular rows


请参阅此处以进行添加动态列.



See here for adding columns dynamically.


egdg写道:

我们需要在数据库中更新...

we need to update in the database...


为此,您需要使用Web服务来更新数据源.


For this you need to use a web service to update your data source.


一个快速的答案是动态添加行您需要使用
ObservableCollection,然后将其与datagrid绑定.

因此,每当您向集合中添加新的空白时,都会在datagrid中添加新行.

XAML文件
One quick answer is for adding rows dynamically you need to use
ObservableCollection and then bind it with the datagrid.

So whenever you add new blank to your collection it will add new row to datagrid.

XAML File
<UserControl .... xmlns:my="clr-namespace:System.Windows.Controls; assembly=System.Windows.Controls.Data>...
<my:DataGrid x:Name="dg" AutoGenerateColumns="True"></my:DataGrid>
<TextBox x:Name="Nametxt" />
<Button Content="Add" Click="Btn_click"/>



用C#代码



In C# Code

List<Data> source = new List<Data>();

Loaded.....
{
    dg.ItemsSource = source;
}

void Btn_Click(...)
{
        source.Add(new Data()
        { 
            Name = Nametxt.Text.Trim();
        });
}




要动态添加列,您需要绑定数据集,而在绑定之前,您需要将列添加到数据集,这又将列添加到datagrid控件.




For adding column dynamically you need to bind dataset and before binding it you need to add column to dataset, which in turn add column to datagrid control.

Dim myconnection As SqlConnection
Dim myda As SqlDataAdapter
Dim ds As DataSet Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Put user code to
initializethepageheremyconnection=NewSqlConnection
"Server=localhost;uid=sa;password=;database=northwind;")
myda = New SqlDataAdapter("Select * from Products", myconnection)
ds = New DataSet()
myda.Fill(ds, "AllTables")
Dim dc As DataColumn
dc = New DataColumn("Total", Type.GetType("System.Double"))
dc.Expression = "UnitPrice * UnitsInStock"
ds.Tables(0).Columns.Add(dc)
DataGrid1.DataSource = ds
DataGrid1.DataBind()
End Sub



HTH



HTH


这篇关于在Silverlight中的datagrid控件中动态添加行和列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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