【问题标题】:Convert WPF form code for WPF Xaml for datagrid to get specific row为数据网格转换 WPF Xaml 的 WPF 表单代码以获取特定行
【发布时间】:2016-01-18 07:14:50
【问题描述】:

我有以下用 WPF 表单编写的代码,并且运行良好。但后来我决定使用 XAML 来获得更好的 GUI 结果。此方法附加到 WPF 表单中的“CellClick”事件。它基本上负责获取选定的行,然后查询它以从数据库中获取数据。现在在 XAML 中没有称为“CellClick”的事件。什么是替代方法以及如何从 XAML 中的数据网格中获取选定的行。

     private void employeeInfo_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        try
        {
            empId = Convert.ToInt32(employeeInfo.Rows[e.RowIndex].Cells["Employee ID"].Value.ToString());
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "SELECT * FROM Employees WHERE [Employee_id]=@employeeId";
            cmd.Parameters.AddWithValue("@employeeId", empId);
            cmd.Connection = con;
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        DataTable dt = new DataTable();
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        sda.Fill(dt);
            foreach(DataRow dr in dt.Rows)
            {
                employeeIDTxt.Text = dr["Employee_Id"].ToString();
                firstNameTxt.Text = dr["First_Name"].ToString();
                lastNameTxt.Text = dr["Last_Name"].ToString();
                addressTxt.Text = dr["Address"].ToString();
                aptTxt.Text = dr["Apt"].ToString();
                stateTxt.Text = dr["State"].ToString();
                cityTxt.Text = dr["City"].ToString();
                zipTxt.Text = dr["Zip"].ToString();
                phoneTxt.Text = dr["Phone"].ToString();
                cellTxt.Text = dr["Cell"].ToString();
                emailTxt.Text = dr["Email"].ToString();
                hourlyPayTxt.Text = dr["Hourly_Pay"].ToString();
                passwordTxt.Text = dr["Password"].ToString();
                statusTxt.Text = dr["Status"].ToString();

            }
        }
        catch (ArgumentOutOfRangeException arg)
        {
            MessageBox.Show("Please select a vlaud Employee" +arg);
        }
        catch (Exception ex)
        {
            MessageBox.Show("WTF happened bro. check it again. Inside the Employee Info method" + ex);
        }
    }

我已尝试使用以下代码从所选行中获取特定列,但问题是此事件被触发两次,第二次引发argumentOutOfBound 异常。

        private void employeeInfo_CellClick(object sender, System.Windows.Controls.SelectedCellsChangedEventArgs e)
    {
        MessageBox.Show("EmployeeInfo method");
        DataRowView dg = (DataRowView)employeeInfo.SelectedItems[0];
        empId = Convert.ToInt32(dg["Employee ID"]);
}

感谢您的帮助。

【问题讨论】:

    标签: c# wpf wpfdatagrid


    【解决方案1】:

    你可以使用 InputBindings

    XAML:

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
        </Grid.RowDefinitions>
        <DataGrid x:Name="grid" ItemsSource="{Binding Items}">
            <DataGrid.InputBindings>
                <MouseBinding MouseAction="LeftClick" Command="{Binding GoCommand}" CommandParameter="{Binding ElementName=grid, Path=SelectedItem}"></MouseBinding>
            </DataGrid.InputBindings>
        </DataGrid>
        <TextBlock Text="{Binding ElementName=grid, Path=SelectedItem}" Grid.Row="1"></TextBlock>
    </Grid>
    

    虚拟机等:

    public class MainViewModel
    {
        public ObservableCollection<TestItem> Items { get; set; } = new ObservableCollection<TestItem> { new TestItem() { Id = 1, Name = "Foo" }, new TestItem() { Id = 2, Name = "Bar" } };
    
        public ICommand GoCommand => new DelegateCommand<TestItem>(Go);
    
        void Go(TestItem selected)
        {
            if (selected != null)
                MessageBox.Show(selected.Name);
        }
    }
    
    public class TestItem
    {
        public int Id { get; set; }
        public string Name { get; set; }
    
        public override string ToString()
        {
            return Name;
        }
    }
    
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
    
            DataContext = new MainViewModel();
        }
    }
    

    请注意,这只是执行此操作的多种方法之一 - 您还可以在 VM 中创建一个属性并将 SelectedItem 绑定到它(Mode=TwoWay)。

    您可能想玩转单元格/网格的可编辑性。

    您可以在 Google 上找到 DelegateCommand 的实现。

    【讨论】:

      猜你喜欢
      • 2011-08-15
      • 1970-01-01
      • 2021-01-27
      • 1970-01-01
      • 2011-02-23
      • 2010-11-12
      • 2015-07-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多