0

I have created listview in C# and filled it with data from SQL server. But when I assign mouse double click, I don't know how to get clicked data. Please help:

My XAML:

<ListView Name="ListViewEmployeeDetails" ItemsSource="{Binding Path=Table}" Margin="0,0,0,67" MouseDoubleClick="ListViewEmployeeDetails_MouseDoubleClick">
    <ListView.Background>
        <LinearGradientBrush>
            <GradientStop Color="white" Offset="0"/>
        </LinearGradientBrush>
    </ListView.Background>
    <ListView.View>
        <GridView>
            <GridViewColumn Width="70" Header="Číslo bytu" DisplayMemberBinding="{Binding Path=cislo_Bytu}"/>
            <GridViewColumn Width="70" Header="Podlaží" DisplayMemberBinding="{Binding Path=podlazi}"/>
            <GridViewColumn Width="70" Header="Účel" DisplayMemberBinding="{Binding Path=ucel}"/>
            <GridViewColumn  Width="70" Header="Plocha" DisplayMemberBinding="{Binding Path=plocha}"/>
            <GridViewColumn Width="70" Header="Stav" DisplayMemberBinding="{Binding Path=stav}"/>
            <GridViewColumn Width="70" Header="Country" DisplayMemberBinding="{Binding Path=Country}"/>
        </GridView>
    </ListView.View>
</ListView>

And my code:

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

String str = "SELECT cislo_Bytu, podlazi, ucel, plocha, stav, poznamky FROM prostory";
cmd.CommandText = str;
ad.SelectCommand = cmd;
cmd.Connection = datovéPřipojení;
DataSet ds = new DataSet();
ad.Fill(ds);

ListViewEmployeeDetails.DataContext = ds.Tables[0].DefaultView;
datovéPřipojení.Close();

So my question is, what I should write into the

private void ListViewEmployeeDetails_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    // Here
}

To get the data (cislo_Bytu) from the row which was clicked on?

Thanks,

4 Answers 4

4

The sender will be the ListView.

((ListView)sender).SelectedItem

Since you are binding to a DataView, the SelectedItem will be of type DataRowView. You can then reference the relevant value using the column name. For example, to assign the value of the cislo_bytu column to the textBox1.Text property, do the following:

textBox1.Text = ((DataRowView)((ListView)sender).SelectedItem)["cislo_bytu"].ToString();
Sign up to request clarification or add additional context in comments.

1 Comment

And if I would like to put that data into textbox? the point is that someone will click on the row and text from one cell (for example cislo_bytu) is going to be shown in a textbox... ? Thanks a lot...
1

I believe what you want is OriginalSource MSDN

e.OriginalSource

Comments

0

Use following code to get the each item by changing the index value in the ItemArray in your double click event

((System.Data.DataRowView)(ListViewEmployeeDetails.SelectedItem)).Row.ItemArray[0]

`

Comments

0

You have to cast the object that is selected to access each proprety..

Example:

((Class)listView1.SelectedItem).cislo_bytu

just change Class to your original class that has proprety cislo_bytu

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.