[RESOLVED] Listview sort issues on creation
I have a list view that I would like to sort on creation. I have been able to sort it but now I am having issues getting it to sort in the desired way. I am wanting to include the cashflowTransaction_id field so that I can remove using the primary key, but I am wanting to sort on the cashflowTransaction_date field.
I have tried various combinations including adjusting the query. However it always populates with the transaction_id field sorted. If I adjust the listview setting to the transaction_date it populates the date in both the transaction id and data columns of the listview.
Code:
private void OverviewListView()
{
try{
overviewListView.Clear();
overviewListView.View = View.Details;
overviewListView.GridLines = true;
overviewListView.FullRowSelect = true;
ColumnHeader columnHeader1 = new ColumnHeader();
overviewListView.Columns.Add("Transaction ID",125);
ColumnHeader columnHeader2 = new ColumnHeader();
overviewListView.Columns.Add("Date",75);
ColumnHeader columnHeader3 = new ColumnHeader();
overviewListView.Columns.Add("Type",75);
ColumnHeader columnHeader4 = new ColumnHeader();
overviewListView.Columns.Add("Business Name",150);
ColumnHeader columnHeader5 = new ColumnHeader();
overviewListView.Columns.Add("Expense Amount",150);
ColumnHeader columnHeader6 = new ColumnHeader();
overviewListView.Columns.Add("Income Amount",150);
ColumnHeader columnHeader7 = new ColumnHeader();
overviewListView.Columns.Add("Income Type",150);
int y = 0;
string overViewQuery = "Select cashflowTransaction_id, cashflowTransaction_date, cashflowTransaction_type, cashflowTransaction_businessName, cashflowTransaction_expenseAmount, cashflowTransaction_incomeAmount, cashflowTransaction_incomeType from cashflowTransaction;";
localDBConn.Open();
SQLiteCommand myOverViewQuery = new SQLiteCommand(overViewQuery,localDBConn);
SQLiteDataAdapter overViewAdaptor = new SQLiteDataAdapter(myOverViewQuery);
DataSet overViewDataSet = new DataSet("myOverView");
overViewAdaptor.Fill(overViewDataSet, "myOverView");
string[] overViewListArray = new string[overViewDataSet.Tables[0].Rows.Count];
foreach(DataRow row in overViewDataSet.Tables[0].Rows)
{
overViewListArray[y] = row["cashflowTransaction_id"].ToString();
ListViewItem overViewListItem = new ListViewItem(row["CashflowTransaction_id"].ToString());
overviewListView.Items.Add(overViewListItem);
overViewListItem.SubItems.Add(row[1].ToString());
overViewListItem.SubItems.Add(row[1].ToString());
overViewListItem.SubItems.Add(row[2].ToString());
overViewListItem.SubItems.Add(row[3].ToString());
overViewListItem.SubItems.Add(row[4].ToString());
overViewListItem.SubItems.Add(row[5].ToString());
y++;
}
//alternate colors of the rows
bool b = true;
foreach (ListViewItem l in overviewListView.Items)
{
if (!b)
{
l.BackColor = Color.LightGray;
b = true;
}
else
b = false;
}
localDBConn.Close();
}
catch
{
//empty catch
localDBConn.Close();
}
}
I am assume that the code in question will be this block of code but I have altered it everyway possible to get it to populate with the date sorted and still have the transaction_id filed visible. Only way I could get it to work was to eliminate the id field, which causes issues when I try to delete rows.
Code:
string[] overViewListArray = new string[overViewDataSet.Tables[0].Rows.Count];
foreach(DataRow row in overViewDataSet.Tables[0].Rows)
{
overViewListArray[y] = row["cashflowTransaction_id"].ToString();
ListViewItem overViewListItem = new ListViewItem(row["CashflowTransaction_date"].ToString());
overviewListView.Items.Add(overViewListItem);
//overViewListItem.SubItems.Add(row[1].ToString());
overViewListItem.SubItems.Add(row[1].ToString());
overViewListItem.SubItems.Add(row[2].ToString());
overViewListItem.SubItems.Add(row[3].ToString());
overViewListItem.SubItems.Add(row[4].ToString());
overViewListItem.SubItems.Add(row[5].ToString());
y++;
}