Pieter Du Toit
Pieter Du Toit

Reputation: 429

wpf xaml loop thru a gridview which is inside a listview and get the cell value

I have the following in my dgridtext.xaml file.

XAML

<Grid x:Name="grid1">
<StackPanel>
    <ListView Name="listview1" IsTextSearchEnabled="True" TextSearch.TextPath="Enquiry_Number">
        <ListView.View>
            <GridView ColumnHeaderToolTip="Multiple Category Information">                        
                <GridViewColumn DisplayMemberBinding="{Binding Path=Enquiry_Number}" Header="Enquiry number"/>
                <GridViewColumn DisplayMemberBinding="{Binding Path=Consignee_Ref}" Header="Consignee reference"/>
                <GridViewColumn DisplayMemberBinding="{Binding Path=Booking_Reference}" Header="Booking reference"/>

            </GridView>
        </ListView.View>                
    </ListView>            
</StackPanel>
</Grid>

Something like this.

dgridtest.xaml.cs

for (int i = 0; i < listview1.Items.Count; i++)
{
    MessageBox.Show(listview1.Items[i].ToString());            
}

But all that returns is System.Data.DataRowView

Please help.

Update:

In My dgridtextxaml.cs file, I call the DataManager.cs class passing a dataset object which is the source of my listview(listview1).

DataManager.BindFilteredData(dts);
listview1.ItemsSource = dts.Tables[0].DefaultView;

And this is what I have in my DataManager.cs class

    public static void BindFilteredData(DataSet dts)
    {
        using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConString"].ConnectionString))
        {
            string sql = "SELECT Enquiry_Number, Consignee_Ref, Booking_Reference FROM ConsHead";

            using (SqlDataAdapter adapter = new SqlDataAdapter(sql, connection))
            {                    
                adapter.Fill(dts);
            }
        }
    }

Upvotes: 1

Views: 1036

Answers (1)

Adrian F&#226;ciu
Adrian F&#226;ciu

Reputation: 12562

You can cast the item to whatever class you use in the ListView and then use any property you need from it. Something like:

var item = listView1.Items[i] as YourClassHere;

Edit

In your case, since you bind the ItemsSource to a DataSet directly you probably can use DataRowView class, and then use the properties from it:

var firstItem = listview1.Items[0] as DataRowView;
var firstCellValue = firstItem.Row[0];

Upvotes: 2

Related Questions