I'm attempting to access a listview subitem. I have to insert columns into this ListView/GridView dynamically as this program accesses multiple databases with different value types. If need be I can portion out the database searching into multiple tabs, but for ease of use, I'd rather not take this route.
I've been searching for roughly 3-4 days for a solution. This class organizes the subitems data from a OleDB query. Here is a snippet of the code I'm using:
public class Repair
{
public string RP { get; set; }
public string SN { get; set; }
public DateTime REC { get; set; }
public DateTime START { get; set; }
public string CUST { get; set; }
public string SP { get; set; }
public string TECH { get; set; }
public string STATUS { get; set; }
public string MODEL { get; set; }
public string NOTES { get; set; }
public DateTime ACCUSED { get; set; }
public string ACCNOTES { get; set; }
public int ID { get; set; }
}
public IList<Repair> OpenRepair { get; set; }
It then inserts the data into the ListView with the following snippet:
var gridView = new GridView();
this.searchListView.View = gridView;
gridView.Columns.Add(new GridViewColumn { Header = "RMA #", DisplayMemberBinding = new System.Windows.Data.Binding("RP") });
gridView.Columns.Add(new GridViewColumn { Header = "Serial", DisplayMemberBinding = new System.Windows.Data.Binding("SN") });
gridView.Columns.Add(new GridViewColumn { Header = "Recieved", DisplayMemberBinding = new System.Windows.Data.Binding("REC") });
gridView.Columns.Add(new GridViewColumn { Header = "Start", DisplayMemberBinding = new System.Windows.Data.Binding("START") });
gridView.Columns.Add(new GridViewColumn { Header = "Customer", DisplayMemberBinding = new System.Windows.Data.Binding("CUST") });
gridView.Columns.Add(new GridViewColumn { Header = "Sales Person", DisplayMemberBinding = new System.Windows.Data.Binding("SP") });
gridView.Columns.Add(new GridViewColumn { Header = "Technician", DisplayMemberBinding = new System.Windows.Data.Binding("TECH") });
gridView.Columns.Add(new GridViewColumn { Header = "Status", DisplayMemberBinding = new System.Windows.Data.Binding("STATUS") });
gridView.Columns.Add(new GridViewColumn { Header = "Repair Notes", DisplayMemberBinding = new System.Windows.Data.Binding("NOTES") });
gridView.Columns.Add(new GridViewColumn { Header = "Accidental Used Date", DisplayMemberBinding = new System.Windows.Data.Binding("ACCUSED") });
gridView.Columns.Add(new GridViewColumn { Header = "Accidental Notes", DisplayMemberBinding = new System.Windows.Data.Binding("ACCNOTES") });
gridView.Columns.Add(new GridViewColumn { Header = "ID", DisplayMemberBinding = new System.Windows.Data.Binding("ID"), Width = 0 });
using (OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=X:\***"))
{
cmd.Connection = cn;
cmd.CommandText = sqlstatement;
try
{
cn.Open();
dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
OpenRepair = new List<Repair>();
if (dr[10].ToString() == "True")
{
OpenRepair.Add(
new Repair()
{
RP = dr[0].ToString(),
SN = dr[1].ToString(),
REC = Convert.ToDateTime(dr[2].ToString()).Date,
START = Convert.ToDateTime(dr[3].ToString()).Date,
CUST = dr[4].ToString(),
SP = dr[5].ToString(),
TECH = dr[6].ToString(),
STATUS = dr[7].ToString(),
MODEL = dr[8].ToString(),
NOTES = dr[9].ToString(),
ACCUSED = Convert.ToDateTime(dr[11].ToString()),
ACCNOTES = dr[12].ToString(),
ID = Convert.ToInt32(dr[13].ToString())
});
}
else
{
OpenRepair.Add(
new Repair()
{
RP = dr[0].ToString(),
SN = dr[1].ToString(),
REC = Convert.ToDateTime(dr[2].ToString()).Date,
START = Convert.ToDateTime(dr[3].ToString()).Date,
CUST = dr[4].ToString(),
SP = dr[5].ToString(),
TECH = dr[6].ToString(),
STATUS = dr[7].ToString(),
MODEL = dr[8].ToString(),
NOTES = dr[9].ToString(),
ID = Convert.ToInt32(dr[13].ToString())
});
}
searchListView.Items.Add(OpenRepair);
}
The XAML behind the ListView:
<ListView ItemsSource="{Binding Repair}" SelectionMode="Single" x:Name="searchListView" Margin="0,63,0,0" Background="DarkGray" MouseDoubleClick="searchListView_MouseDoubleClick">
<ListView.View>
<GridView />
</ListView.View>
</ListView>
I've attempted to grab the Subitem values within the ListView/GridView with a few approaches. It has not been successful on either of these codes:
Repair lvi = (Repair)searchListView.SelectedItems[0];
System.Windows.MessageBox.Show(lvi.RP + " " + lvi.SN + " " + lvi.SP);
&
Repair lvi = (Repair)this.customersListView.SelectedItem;
MessageBox.Show(string.Format("Repair: {1}{0}Serial Number:{2}", Environment.NewLine, lvi.RP, lvi.SN));
Even attempted a Windows Form approach, which obviously did not work.
Could someone please at least point me in the correct direction? The error message that I've been obtaining makes sense being it cannot be cast from the object Generic List to class Repair, however, since I am fairly new to WPF, I'm stumped on how to get passed this message!
Following my comment, here is a more complete answer, for future reference.
The selected object (the one displayed by the SelectedItem
) is available through SelectedValue
.
And as you use List
then you should do the following:
((List<Repair>)searchListView.SelectedValue)[0].SN