开发者

How to retrieve selected ListView value when data comes from SQL Server (WPF)

开发者 https://www.devze.com 2023-02-05 22:43 出处:网络
I\'m new to C# and WPF so hopefully this is a simple issue. I have a listview which retrieves data from a SQL Server table.

I'm new to C# and WPF so hopefully this is a simple issue.

I have a listview which retrieves data from a SQL Server table.

<ListView Width="450" Margin="10" Grid.Row="1" ItemsSource="{Binding Path=Table}" Name="FIData" SelectionChanged="FIData_SelectionChanged">
       <ListView.View>
         <GridView>
           <GridViewColumn Header="FI #" DisplayMemberBinding="{Binding Path=Id}"/>
           <GridViewColumn Header="FI Short Name" DisplayMemberBinding="{Binding Path=ShortName}"/>
           <GridViewColumn Header="FI Long Name" DisplayMemberBinding="{Binding Path=LongName}"/>
           <GridViewColumn Header="Last Load Date" DisplayMemberBinding="{Binding Path=MostRecentEffectiveDate}"/>
         </GridView>
       </ListView.View>
    </ListView>

SqlConnection con = new SqlConnection();
    SqlDataAdapter ad = new SqlDataAdapter();
    SqlCommand cmd = new SqlCommand();

    string strSql = "SELECT Id, ShortName, LongName, MostRecentEffectiveDate FROM Institutions";


public void BindData()
    {
        cmd.CommandText = strSql;
        ad.SelectCommand = cmd;
        con.ConnectionString = "Data Source=DEVDB\\ABC; Initial Catalog=Cat1; Integrated Security=True";
        cmd.Connection = con;
        DataSet ds = new DataSet();
        ad.Fill(ds);
        FIData.DataContext = ds.Tables[0].DefaultView;
        con.Close();
    }

private void Button_Click(object sender, RoutedEventArgs e)
  开发者_如何学Python  {
        BindData();
    }

When clicking a button, the BindData method is called and my list view populates just fine. My problem is I don't know how to retrieve the data values from the selected list item.

In all of the other examples users are either populating their data from properties in the C# code or XML or some other source. I'm thinking that perhaps one of my problems is I don't understand objects quite right, but I was hoping someone could show me how I go about getting specific values from the selected list. I want to use one of those values later on.

Thanks,

Jason


Here, when you do:

FIData.DataContext = ds.Tables[0].DefaultView;

it maps the actual DataTable with virtual properties, which can be used for data-binding scenarios like yours.

But to answer your question on how you can retrieve the data values, I think you are pretty close because you have FIData_SelectionChanged event for your listview. In that event:

private void FIData_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
  var myListView = sender as ListView;
  if(myListView != null)
  {
    var selectedItem = myListView.SelectedItem;

    //selectedItem should have an array, which contains data values for the bound object.
  }
}

you should inspect the selectedItem above in the debugger and look at what data it contains.

0

精彩评论

暂无评论...
验证码 换一张
取 消