DataGrid实例(简单易懂,无复杂功能,适合初学者)

使ACCESS数据库,适合初学者,修改连接、查询语句后可直接运行,代码中有注明。

      填充DataSet的步骤
      1、使用数据库连接字符串创建数据库连接对象
      2、用SQL查询语句和数据库连接对象创建数据库适配器dataAdapter
      3、使用DataAdapter的Fill 方法填充DataSet

using  System;
using  System.Windows.Forms;
using  System.Data;
using  System.Data.SqlClient;
using  System.Data.OleDb;
// Professional C# 2nd的DATAGRID实例
///    <summary>
///    This class provides    an example of creating and using a data    grid.
///    </summary>

public   class  DisplayTabularData : System.Windows.Forms.Form
{
    
private System.Windows.Forms.Button retrieveButton;
    
private System.Windows.Forms.DataGrid dataGrid;

    
///    <summary>
    
///    Construct the window.
    
///    </summary>
    
///    <remarks>
    
///    This method    constructs the window by creating both the data    grid and the button.
    
///    </remarks>

    public DisplayTabularData()
    
{
        
this.AutoScaleBaseSize = new System.Drawing.Size(513);
        
this.ClientSize = new System.Drawing.Size(464253);
        
this.Text = "01_DisplayTabularData";
        
this.dataGrid = new System.Windows.Forms.DataGrid();
        dataGrid.BeginInit();
        dataGrid.Location 
= new System.Drawing.Point(88);
        dataGrid.Size 
= new System.Drawing.Size(448208);
        dataGrid.TabIndex 
= 0;
        dataGrid.Anchor 
= AnchorStyles.Bottom | AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
        
this.Controls.Add(this.dataGrid);
        dataGrid.EndInit();
        
this.retrieveButton = new System.Windows.Forms.Button();
        retrieveButton.Location 
= new System.Drawing.Point(384224);
        retrieveButton.Size 
= new System.Drawing.Size(7523);
        retrieveButton.TabIndex 
= 1;
        retrieveButton.Anchor 
= AnchorStyles.Bottom | AnchorStyles.Right;
        retrieveButton.Text 
= "Retrieve";
        retrieveButton.Click 
+= new System.EventHandler(this.retrieveButton_Click);
        
this.Controls.Add(this.retrieveButton);
    }


    
///    <summary>
    
///    Retrieve the data
    
///    </summary>
    
///    <param name="sender"> </param>
    
///    <param name="e"> </param>

    protected void retrieveButton_Click(object sender, System.EventArgs e)
    
{
        retrieveButton.Enabled 
= false;

        
string source = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/Documents and Settings/Manio/My Documents/PrintManager/program/PrintManageV1/DataBase/PrintDB.mdb";
        
string select = "SELECT * FROM MainInfo";

        
////
        //填充DataSet的步骤
        
//1、使用数据库连接字符串创建数据库连接对象
        
//    2、用SQL查询语句和数据库连接对象创建数据库适配器dataAdapter
        
//        3、使用DataAdapter的Fill 方法填充DataSet

        OleDbConnection OleCon 
= new OleDbConnection(source);

        OleDbDataAdapter da 
= new OleDbDataAdapter(select,OleCon);

        DataSet ds 
= new DataSet();

        da.Fill(ds, 
"MainInfo");

        dataGrid.SetDataBinding(ds, 
"MainInfo");    //DataGrid的数据绑定,使用DataSet 和 数据库的表名
    }


    
///    <summary>
    
///    Display    the    application    window
    
///    </summary>

    static void Main()
    
{
        Application.Run(
new DisplayTabularData());
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现WPF DataGrid的全选功能,可以在DataGrid的Header中添加一个CheckBox,当该CheckBox被选中时,遍历DataGrid的所有行,并将每行的CheckBox选中。 以下是一个示例代码,实现了DataGrid的全选功能: ```xml <DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTemplateColumn Header=""> <DataGridTemplateColumn.HeaderTemplate> <DataTemplate> <CheckBox x:Name="chkSelectAll" Click="chkSelectAll_Click"/> </DataTemplate> </DataGridTemplateColumn.HeaderTemplate> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> <DataGridTextColumn Header="Name" Binding="{Binding Name}" /> <DataGridTextColumn Header="Age" Binding="{Binding Age}" /> </DataGrid.Columns> </DataGrid> ``` 在代码中,我们为DataGrid添加了一个模板列,该模板列包含一个CheckBox。在HeaderTemplate中,我们使用了一个命名的CheckBox,用于在后续代码中引用。 接下来,在DataGrid的DataContext中,我们需要添加一个IsSelected属性,用于存储每行CheckBox的选中状态: ```csharp public class ItemViewModel : INotifyPropertyChanged { private bool _isSelected; public bool IsSelected { get { return _isSelected; } set { _isSelected = value; OnPropertyChanged("IsSelected"); } } public string Name { get; set; } public int Age { get; set; } public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } public class MainWindowViewModel { public ObservableCollection<ItemViewModel> Items { get; set; } public MainWindowViewModel() { Items = new ObservableCollection<ItemViewModel>() { new ItemViewModel() { Name = "John", Age = 30 }, new ItemViewModel() { Name = "Jane", Age = 25 }, new ItemViewModel() { Name = "Bob", Age = 35 } }; } } ``` 最后,在代码中添加Click事件处理程序,用于处理CheckBox的选中状态: ```csharp private void chkSelectAll_Click(object sender, RoutedEventArgs e) { var chk = sender as CheckBox; foreach (var item in Items) { item.IsSelected = chk.IsChecked.Value; } } ``` 在事件处理程序中,我们首先获取发送事件的CheckBox。然后,我们遍历DataGrid的所有行,并将每行的CheckBox选中状态设置为发送事件的CheckBox的选中状态。 现在,当用户选中DataGrid的Header中的CheckBox时,所有行的CheckBox将被选中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值