如何将等效的datagridview更改为wpf datagrid? [英] How to change the equivalent datagridview to wpf datagrid?

查看:78
本文介绍了如何将等效的datagridview更改为wpf datagrid?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面我使用的是datagridview中的代码,它的工作正常,但是我需要如何将相应的代码更改为wpf datagrid。帮助我



Here below am used the code in datagridview and its works fine,but nw i need how to change the corresponding code to wpf datagrid.Help me

private int GetRowIndex(string ID)
{
    int num = -1;
    //Get the row based on StudyID(here as parameter ID)
    foreach (DataGridViewRow dataGridVV in (IEnumerable)this.dataGridView.Cells)
    {
        if (dataGridVV.Cells[0].Value.Equals((object)ID))
            num = dataGridVV.Index;
    }
    return num;
}

推荐答案

嗨会员,



此代码你显示的不是很安全 - 你说单元格[0]有索引,字符串比较等等。我希望你不要过分依赖这个索引的东西。



无论如何,我认为你有理由使用这些代码...



在WPF你通常会使用Binding将列表(一些枚举)绑定到 DataGrid 。所以我使用Linq在您的底层数据源中找到所需的记录(行),然后您可以使用 ItemCollection <的 IndexOf 方法/ code>。



此代码显示了这个想法 - 如果您使用以下内容替换新WPF项目中的MainWindow,则可以运行它。



Hi Member,

This code you are showing is not very safe - you say the cell[0] has the index, string comparison etc. I hope you don't rely heavily on this "index" thing.

Anyway, I assume you have got reasons to use such code...

In WPF you would normally use Binding to bind a list (some enumeration whatever) to your DataGrid. So I'd use Linq to find the desired record (row) in your underlaying datasource, then you can just use the IndexOf method of the ItemCollection.

This code shows the idea - you can run it if you replace MainWindow in a new WPF project with the following.

<Window x:Class="WpfApplication2.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>
        <DataGrid x:Name="m_datagrid"/>
        <Button Content="Button" HorizontalAlignment="Left" Margin="422,283,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>

    </Grid>
</Window>




   public partial class MainWindow : Window
    {
        class Record
        {
            public string Key { get; set; }
            public string Value { get; set; }
        }

        List<Record> m_list = new List<Record> 
        {
            new Record { Key = "A", Value = "aaa"},
            new Record { Key = "B", Value = "bbb"},
            new Record { Key = "C", Value = "ccc"}
        };

        public MainWindow()
        {
            InitializeComponent();

            m_datagrid.ItemsSource = m_list;

        }

        int GetRowIndex(string strKey)
        {
            int iIndex = -1;

            // search the record given by the key (id) in the underlaying datasource (a simple List in this case)
            Record record = m_list.FirstOrDefault(r => r.Key == strKey);
            // get the index of the found record (or -1 if it wasn't found)
iIndex = m_datagrid.Items.IndexOf(record);

            return iIndex;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(this, GetRowIndex("C").ToString());

        }





只是我的2c - 也许它可以帮到你。



快乐编码!



Just my 2c - maybe it helps you.

Happy coding!


这篇关于如何将等效的datagridview更改为wpf datagrid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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