这是我的列表项类
public class ListItem
{
private string _FieldName = "";
private string _FieldDesc = "";
private string _TableName = "";
public ListItem(string FieldName, string FieldDesc, string TableName)
{
this.FieldName = FieldName;
this.FieldDesc = FieldDesc;
this.TableName = TableName;
}
public string FieldName
{
get
{
return _FieldName;
}
set
{
_FieldName = value;
}
}
public string FieldDesc
{
get
{
return _FieldDesc;
}
set
{
_FieldDesc = value;
}
}
public string TableName
{
get
{
return _TableName;
}
set
{
_TableName = value;
}
}
//public override string ToString()
//{
// return this.name;
//}
}
我虽然可以将dgList.Rows [i] .Cells [0]转换为我自己的listitem类。
这里我在尝试读取for循环中的单元格值时收到错误。在这里,我尝试施放和读取值。这是我的代码。当按钮单击时,我尝试读取for循环中的单元格值。 这是代码
private void Form2_Load(object sender, EventArgs e)
{
ArrayList info = new ArrayList();
info.Add(new ListItem("JID", "Job ID", "BBAJobs"));
info.Add(new ListItem("AccountReference", "Customer ID", "BBAJobs"));
info.Add(new ListItem("WID", "WID", "BBAJobs"));
DataTable dg = new DataTable();
dg.Columns.Add("data", typeof(string));
DataRow drg = null;
drg = dg.NewRow();
drg[0] = "";
dg.Rows.Add(drg);
dgList.DataSource = dg;
DataGridViewComboBoxColumn dgcoSpec = new DataGridViewComboBoxColumn();
dgcoSpec.DataSource = info;
dgcoSpec.DisplayMember = "FieldDesc";
dgcoSpec.ValueMember = "FieldName";
dgcoSpec.AutoComplete = true;
dgcoSpec.HeaderText = "Fields";
dgcoSpec.Name = "Fields";
dgcoSpec.DefaultCellStyle.NullValue = "--Select--";
dgcoSpec.Width = 115;
dgList.Columns.Insert(0, dgcoSpec);
}
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i <= dgList.Rows.Count - 1; i++)
{
ListItem lst= (ListItem) dgList.Rows[i].Cells[0];
string x1 = lst.FieldName;
string x2 = lst.FieldDesc;
string x3 = lst.TableName;
}
}
请告诉我如何使用代码段进行操作。
感谢