这在DataGrid控件中可行吗?

时间:2012-06-26 16:29:44

标签: c# wpf winforms datagrid

我正在规划软件(供私人使用)。应用程序的主要功能是数据网格。

我将有一个向数据网格添加新行的按钮。我正在考虑直接在数据网格中添加行,而不是添加另一个窗口来添加数据。换句话说,当我单击“新建”时,新行将直接出现在可编辑的数据网格中,直到我单击“保存”。在编辑过程中,某些字段可能会出现下拉菜单。这可以使用C#与Windows窗体或WPF实现吗?如果可能,演示代码将是有用的。

3 个答案:

答案 0 :(得分:0)

这是使用数据绑定的简短示例:*

在XAML中

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Margin="0,0,417,277" Click="Button_Click">ADD</Button>
        <DataGrid ItemsSource="{Binding MyDataTable}" Margin="0,40,0,21" CanUserAddRows="False">
        </DataGrid>
    </Grid>
</Window>
代码中的

.cs:

using System;
using System.Windows;
using System.Data;
using System.ComponentModel;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window , INotifyPropertyChanged
    {

        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;

            MyDataTable = new DataTable();
            MyDataTable.Columns.Add("Column1");
            MyDataTable.Columns.Add("Column2");

        }
        private DataTable _MyDataTable;
        public DataTable MyDataTable
        {
            get
            {
                return _MyDataTable;
            }
            set
            {
                _MyDataTable = value;
                NotifyPropertyChanged("MyDataTable");
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            DataRow row = MyDataTable.NewRow();

            MyDataTable.Rows.Add(row);


        }



        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
    }
}

答案 1 :(得分:0)

这是一个工作示例,应该为您提供实现此目的的跳板:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <Button Content="Add New Item" x:Name="myButton" Click="myButton_Click" />
        <DataGrid x:Name="myGrid" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" CanUserAddRows="False" />
    </StackPanel>
</Window>

代码隐藏:(不是代码的粉丝,但是对于一个快速而肮脏的例子)

public partial class MainWindow : Window
{

    ObservableCollection<MyViewModel> guys { get; set; }

    public MainWindow()
    {
        InitializeComponent();
        guys = new ObservableCollection<MyViewModel>();
        guys.Add(new MyViewModel { Name = "First Guy", Age = 21, FavoriteColor = "Blue" });
        guys.Add(new MyViewModel { Name = "Second Guy", Age = 22, FavoriteColor = "Red" });
        guys.Add(new MyViewModel { Name = "Third Guy", Age = 23, FavoriteColor = "Yellow" });
        guys.Add(new MyViewModel { Name = "Fourth Guy", Age = 24, FavoriteColor = "Green" });
        myGrid.ItemsSource = guys;
    }

    public class MyViewModel
    {
        public String Name { get; set; }
        public int Age { get; set; }
        public String FavoriteColor { get; set; }
    }

    private void myButton_Click(object sender, RoutedEventArgs e)
    {
        guys.Add(new MyViewModel { Name = "[New Guy]", Age = 0, FavoriteColor = "[Color]" });
    }

}

希望这有帮助!

答案 2 :(得分:0)

以下是Windows窗体的示例代码。您必须创建一个表单,将DataGridView组件添加到其中(使用您选择的某些列)。添加btnNew和btnSave按钮并绑定到相应的click -events。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        // Hide row headers
        dataGridView1.RowHeadersVisible = false;
    }

    // Add new row to grid
    private void btnNew_Click(object sender, EventArgs e)
    {
        // Insert new row as the first row
        dataGridView1.Rows.Insert(0, new DataGridViewRow());

        // Set the current cell to the first cell in the newly added row
        dataGridView1.CurrentCell = dataGridView1.Rows[0].Cells[0];

        // Begin edit so that the focus goes straight to the new row
        dataGridView1.BeginEdit(false);
    }

    // "Save" (make the rows non editable)
    private void btnSave_Click(object sender, EventArgs e)
    {
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {                
            // Set readonly so the user won't be able to edit these rows anymore
            row.ReadOnly = true;
        }
    }
}