Add ListBox items from Dataset
I have this C# code.
j = myAccountDataset.Tables["AccountsTables"].Rows.Count;
for (i = 0; i <= (j - 1); i++ )
{
listAccountList.Items.Add(myAccountDataset.Tables[0].Rows[i][1]);
}
this.listAccountList.SelectedInde开发者_如何学Cx = 0;
the idea is to iterate inside the dataset and add the items to the list. but i am getting the following errors: Error 1 The best overloaded method match for 'System.Web.UI.WebControls.ListItemCollection.Add(string)' has some invalid arguments
Argument1: cannot convert from 'object' to 'string'
i must be doing something wrong. thye error is in the line: listAccountList.Items.Add(myAccountDataset.Tables[0].Rows[i][1]);
thank you.
The ListItemCollection's Add method only accepts two types - a string or a ListItem See the MSDN documentation here. You need to pass a string instead of an object:
listAccountList.Items.Add(myAccountDataset.Tables[0].Rows[i][1].ToString());
A little more description
Your myAccountDataset.Tables[0].Rows[i][1] is a typeless object, the Add method is expecting a string, you will need to cast the object to a string. The simplest method of doing this is to add the .ToString() operator to your datarow object
myAccountDataset.Tables[0].Rows[i][1].Tostring()
精彩评论