Listview checkboxes

  • Thread starter Thread starter Dean Bortell
  • Start date Start date
D

Dean Bortell

I have a field in an access database that indicates if a
message is new. I have created a listview in vs .net
2003 and set checkboxes to true. when i try to check
each row in a datset and set individual listview items
state to checked/unchecked i get an error that says index
out of bounds. what is going on? here is the bad part
of my code:
private void bindList()
{
try
{

if (oleDbConnection1.State.Equals
(System.Data.ConnectionState.Closed))
oleDbConnection1.Open();
callInfoDS1.Clear();
callInfoDA.Fill(callInfoDS1.Tables["callInfo"]);
messageList.Items.Clear();
string[] s = new string[6];
int i = 0;
foreach (DataRow r in callInfoDS1.Tables["callInfo"].Rows)
{
s[0] = r["callDate"].ToString();
if (r["callRemarks"].ToString() != "")
s[1] = r["callRemarks"].ToString();
if (r["callFor"].ToString() != "")
s[2] = r["callFor"].ToString();
s[3] = r["callStart"].ToString();
s[4] = r["callInterval"].ToString();
s[5] = r["callNumber"].ToString();
ListViewItem l = new ListViewItem(s);
messageList.Items.Add(l);
int x = messageList.Items.Count-1;
if (r["callNew"].Equals(true))
messageList.Items[x].Checked=true;
i++;
}
}
catch (Exception bList)
{
MessageBox.Show(bList.Message);
}
}//end function
 
Hi!

The "messageList.Items.Add" method shall return you a reference to the
ListViewItem object for the ListViewItem just added. Use the Index property
of the returned reference to get the "Correct" zero-based index to make it
checked. Or simply use the "Checked" property of the returned reference to
make it checked.

Regards,
Kumar Gaurav Khanna

--
----------------------------------------------------------------------------
----------
Microsoft MVP - .NET, MCSE Windows 2000/NT4, MCP+I
WinToolZone - Spelunking Microsoft Technologies
http://www.wintoolzone.com/
OpSupport - Spelunking Rotor
http://opsupport.sscli.net/
 
Back
Top