0

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

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

4

3 回答 3

0

这是 Windows 窗体的示例代码。您必须创建一个表单,在其中添加一个 DataGridView 组件(您可以选择一些列)。还添加 btnNew 和 btnSave 按钮并绑定到相应的单击事件。

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;
        }
    }
}
于 2012-06-26T18:52:14.993 回答
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]" });
    }

}

希望这可以帮助!

于 2012-06-26T17:48:42.743 回答
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));
            }
        }
    }
}
于 2012-06-26T16:58:02.130 回答