Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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

C#
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;
}
Posted
Updated 7-Oct-14 1:56am
v2
Comments
Member 11134678 7-Oct-14 8:49am    
Hav anyone give solution plz

1 solution

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.

XML
<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>

C#
   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());

        }


Just my 2c - maybe it helps you.

Happy coding!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900