当有两个 oledbadapter 填充命令时,c# ListBox 项目被 item.tostring() (?) 替换
c# ListBox items get replaced by item.tostring() (?) when having two oledbadapter fill commands
我关于 Whosebug 的第一个问题,因为我还没有找到任何解决方案:
我正在开发一个简单的密码显示工具,它从 MS Access 数据库中获取数据(没有合适的数据库可用...)
Access 数据库有两个 table。 PC table 和密码 child table,通过 MAC 地址链接。
程序在列表框中显示一个 key/value 列表,其中包含通过 oledb 检索到的所有 PC 名称。
key是PC名,value是MAC地址。
这很好用。
void cmdGetPCs()
{
OleDbDataAdapter daPCs = new OleDbDataAdapter();
OleDbConnection vcon = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;data source=H:\XXX\XXX\MYACCESSFILE.accdb;Jet OLEDB:Database Password=[REDACTED]");
const string q = "SELECT SISSI & ' (' & IP & ') - ' & DESCRIPTION as LONGDESCR, MAC from PC_LIST WHERE active = true order by sissi, ip"; //use & instead of + to have blankspace instead of null values for displaying PCs without Sissi because CONCAT doesn't work (MS Access....)
vcon.Open();
daPCs.SelectCommand = new OleDbCommand(q, vcon);
vcon.Close();
DataSet dsPC = new DataSet("PCs");
daPCs.MissingSchemaAction = MissingSchemaAction.AddWithKey;
daPCs.Fill(dsPC, "tblPCs");
DataTable dtPC = dsPC.Tables["tblPCs"];
var PCList = new List<PCInfo>();
foreach (DataRow dtRow in dtPC.Rows)
{
PCList.Add(new PCInfo() { LONGDESCR = dtRow["LONGDESCR"].ToString(), MAC = dtRow["MAC"].ToString() });
}
lstPCs.DisplayMember = "LONGDESCR";
lstPCs.ValueMember = "MAC";
lstPCs.DataSource = PCList;
}
好的,我有一个包含 PC 描述的列表。
现在,当我 select 列表框中的一个项目时,我在同一个 window 中有一个 datagridview 元素,应该用密码信息填充:
void ListBox1SelectedIndexChanged(object sender, EventArgs e)
{
groupBox1.Text = lstPCs.GetItemText(lstPCs.SelectedItem);
string x = lstPCs.GetItemText(lstPCs.SelectedValue);
//Dataset_get(x);
}
只要 Dataset_get 被注释掉,这仍然可以正常工作。第一行将项目框的可见键显示为组框的 header,字符串 x 是 selected 项目的 MAC 地址(=ItemList 值)。
一旦我激活 Dataset_get(x) 并启动程序,列表框就会被填充,但所有项目似乎都被 item.toString() 占位符替换。
列表框看起来像:
- myprogramname.MainForm+PCInfo
- myprogramname.MainForm+PCInfo
- myprogramname.MainForm+PCInfo
- myprogramname.MainForm+PCInfo
- myprogramname.MainForm+PCInfo
- etc..
indexchange 的前两行(groupbox 和字符串 x)仍然可以正常工作并显示正确的值。
此外 Dataset_get 本身工作正常并填充 Datagridview。所以我可以或者有一个没有填充数据网格视图的用户可读列表框或者一个带有填充数据网格视图的损坏列表框...
显然我需要一个带有填充数据网格视图的可读列表框 ;)
我将 Dataset_get 中的问题缩小到一旦调用数据网格视图数据检索的填充行就开始崩溃的程度:
private void Dataset_get(string mymac)
{
OleDbDataAdapter daPass = new OleDbDataAdapter();
OleDbConnection vconp = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;data source=H:\XXX\XXX\MYACCESSFILE.accdb;Jet OLEDB:Database Password=[REDACTED]");
string qp = "";
switch (mypermissions)
{
case "ADMIN":
qp = "SELECT USER_TYPE, HAS_ADMIN, USER_NAME, PASSWORD, ID FROM PASSWORDS WHERE ID = '" + mymac + "' ORDER BY user_type";break;
case "USER":
qp = "SELECT p.USER_TYPE, p.HAS_ADMIN, p.USER_NAME, p.PASSWORD, p.ID FROM PASSWORDS p, PC_LIST pc WHERE p.ID = '" + mymac + "' and p.ID = pc.MAC and (pc.x_plant like '%USER%' or (ucase(p.user_type) not like '%ADMIN%')) ORDER BY p.user_type";break;
default: break;
}
vconp.Open();
daPass.SelectCommand = new OleDbCommand(qp, vconp);
vconp.Close();
DataSet dsPass = new DataSet("Passwords");
daPass.MissingSchemaAction = MissingSchemaAction.AddWithKey;
daPass.Fill(dsPass,"tblPass"); //REPLACEMENT OF LIST ITEMS IS TRIGGERED BY THIS LINE
DataTable dtPass = dsPass.Tables["tblPass"];
dataGridView1.DataSource = dtPass;
}
请帮助我...提前致谢!
PS:datagridview 在组框中,而列表框不在,但我想这没有任何区别。
在 Get the value for a listbox item by index
的 Reza Aghaeis 回答的帮助下解决了这个问题
public static class ListControlExtensions
{
public static object GetItemValue(this ListControl list, object item)
{
if (item == null)
throw new ArgumentNullException("item");
if (string.IsNullOrEmpty(list.ValueMember))
return item;
var property = TypeDescriptor.GetProperties(item)[list.ValueMember];
if (property == null)
throw new ArgumentException(
string.Format("item doesn't contain '{0}' property or column.",
list.ValueMember));
return property.GetValue(item);
}
}
//.........................................
void cmdGetPCs()
{
const string q = "SELECT SISSI & ' (' & IP & ') - ' & DESCRIPTION as LONGDESCR, MAC from PC_LIST WHERE active = true order by sissi, ip"; //use & instead of + to have blankspace instead of null values for displaying PCs without Sissi because CONCAT doesn't work (MS Access....)
OleDbDataAdapter da = new OleDbDataAdapter();
DataSet ds = new DataSet();
vcon.Open();
da.SelectCommand = new OleDbCommand(q, vcon);
vcon.Close();
da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
da.Fill(ds, "tblPCs");
DataTable dtPC = ds.Tables["tblPCs"];
foreach (DataRow dtRow in dtPC.Rows)
{
lstPCs.Items.Add(new KeyValuePair<String, String>(dtRow["LONGDESCR"].ToString(), dtRow["MAC"].ToString()));
}
lstPCs.DisplayMember = "Key";
lstPCs.ValueMember = "Value";
}
//.........................................
private void Dataset_get(string mymac)
{
string qp = "";
switch (mypermissions)
{
case "ADMIN":
qp = "SELECT USER_TYPE, HAS_ADMIN, USER_NAME, PASSWORD FROM PASSWORDS WHERE ID = '" + mymac + "' ORDER BY user_type";break;
case "USER":
qp = "SELECT p.USER_TYPE, p.HAS_ADMIN, p.USER_NAME, p.PASSWORD FROM PASSWORDS p, PC_LIST pc WHERE p.ID = '" + mymac + "' and p.ID = pc.MAC and (pc.x_plant like '%USER%' or (ucase(p.user_type) not like '%ADMIN%')) ORDER BY p.user_type";break;
default: break;
}
OleDbDataAdapter dapass = new OleDbDataAdapter();
DataSet dspass = new DataSet();
vcon.Open();
dapass.SelectCommand = new OleDbCommand(qp, vcon);
vcon.Close();
dapass.MissingSchemaAction = MissingSchemaAction.AddWithKey;
dapass.Fill(dspass,"tblPass");
DataTable dtPass = new DataTable();
dtPass = dspass.Tables["tblPass"];
dataGridView1.DataSource = dtPass;
}
//.........................................
void ListBox1SelectedIndexChanged(object sender, EventArgs e)
{
groupBox1.Text = lstPCs.GetItemText(lstPCs.SelectedItem);
string x = lstPCs.GetItemValue(lstPCs.SelectedItem).ToString();
Dataset_get(x);
}
OleDbConnection vcon 已共享。
我关于 Whosebug 的第一个问题,因为我还没有找到任何解决方案:
我正在开发一个简单的密码显示工具,它从 MS Access 数据库中获取数据(没有合适的数据库可用...)
Access 数据库有两个 table。 PC table 和密码 child table,通过 MAC 地址链接。
程序在列表框中显示一个 key/value 列表,其中包含通过 oledb 检索到的所有 PC 名称。
key是PC名,value是MAC地址。 这很好用。
void cmdGetPCs()
{
OleDbDataAdapter daPCs = new OleDbDataAdapter();
OleDbConnection vcon = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;data source=H:\XXX\XXX\MYACCESSFILE.accdb;Jet OLEDB:Database Password=[REDACTED]");
const string q = "SELECT SISSI & ' (' & IP & ') - ' & DESCRIPTION as LONGDESCR, MAC from PC_LIST WHERE active = true order by sissi, ip"; //use & instead of + to have blankspace instead of null values for displaying PCs without Sissi because CONCAT doesn't work (MS Access....)
vcon.Open();
daPCs.SelectCommand = new OleDbCommand(q, vcon);
vcon.Close();
DataSet dsPC = new DataSet("PCs");
daPCs.MissingSchemaAction = MissingSchemaAction.AddWithKey;
daPCs.Fill(dsPC, "tblPCs");
DataTable dtPC = dsPC.Tables["tblPCs"];
var PCList = new List<PCInfo>();
foreach (DataRow dtRow in dtPC.Rows)
{
PCList.Add(new PCInfo() { LONGDESCR = dtRow["LONGDESCR"].ToString(), MAC = dtRow["MAC"].ToString() });
}
lstPCs.DisplayMember = "LONGDESCR";
lstPCs.ValueMember = "MAC";
lstPCs.DataSource = PCList;
}
好的,我有一个包含 PC 描述的列表。 现在,当我 select 列表框中的一个项目时,我在同一个 window 中有一个 datagridview 元素,应该用密码信息填充:
void ListBox1SelectedIndexChanged(object sender, EventArgs e)
{
groupBox1.Text = lstPCs.GetItemText(lstPCs.SelectedItem);
string x = lstPCs.GetItemText(lstPCs.SelectedValue);
//Dataset_get(x);
}
只要 Dataset_get 被注释掉,这仍然可以正常工作。第一行将项目框的可见键显示为组框的 header,字符串 x 是 selected 项目的 MAC 地址(=ItemList 值)。
一旦我激活 Dataset_get(x) 并启动程序,列表框就会被填充,但所有项目似乎都被 item.toString() 占位符替换。
列表框看起来像:
- myprogramname.MainForm+PCInfo
- myprogramname.MainForm+PCInfo
- myprogramname.MainForm+PCInfo
- myprogramname.MainForm+PCInfo
- myprogramname.MainForm+PCInfo
- etc..
indexchange 的前两行(groupbox 和字符串 x)仍然可以正常工作并显示正确的值。 此外 Dataset_get 本身工作正常并填充 Datagridview。所以我可以或者有一个没有填充数据网格视图的用户可读列表框或者一个带有填充数据网格视图的损坏列表框... 显然我需要一个带有填充数据网格视图的可读列表框 ;)
我将 Dataset_get 中的问题缩小到一旦调用数据网格视图数据检索的填充行就开始崩溃的程度:
private void Dataset_get(string mymac)
{
OleDbDataAdapter daPass = new OleDbDataAdapter();
OleDbConnection vconp = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;data source=H:\XXX\XXX\MYACCESSFILE.accdb;Jet OLEDB:Database Password=[REDACTED]");
string qp = "";
switch (mypermissions)
{
case "ADMIN":
qp = "SELECT USER_TYPE, HAS_ADMIN, USER_NAME, PASSWORD, ID FROM PASSWORDS WHERE ID = '" + mymac + "' ORDER BY user_type";break;
case "USER":
qp = "SELECT p.USER_TYPE, p.HAS_ADMIN, p.USER_NAME, p.PASSWORD, p.ID FROM PASSWORDS p, PC_LIST pc WHERE p.ID = '" + mymac + "' and p.ID = pc.MAC and (pc.x_plant like '%USER%' or (ucase(p.user_type) not like '%ADMIN%')) ORDER BY p.user_type";break;
default: break;
}
vconp.Open();
daPass.SelectCommand = new OleDbCommand(qp, vconp);
vconp.Close();
DataSet dsPass = new DataSet("Passwords");
daPass.MissingSchemaAction = MissingSchemaAction.AddWithKey;
daPass.Fill(dsPass,"tblPass"); //REPLACEMENT OF LIST ITEMS IS TRIGGERED BY THIS LINE
DataTable dtPass = dsPass.Tables["tblPass"];
dataGridView1.DataSource = dtPass;
}
请帮助我...提前致谢!
PS:datagridview 在组框中,而列表框不在,但我想这没有任何区别。
在 Get the value for a listbox item by index
的 Reza Aghaeis 回答的帮助下解决了这个问题 public static class ListControlExtensions
{
public static object GetItemValue(this ListControl list, object item)
{
if (item == null)
throw new ArgumentNullException("item");
if (string.IsNullOrEmpty(list.ValueMember))
return item;
var property = TypeDescriptor.GetProperties(item)[list.ValueMember];
if (property == null)
throw new ArgumentException(
string.Format("item doesn't contain '{0}' property or column.",
list.ValueMember));
return property.GetValue(item);
}
}
//.........................................
void cmdGetPCs()
{
const string q = "SELECT SISSI & ' (' & IP & ') - ' & DESCRIPTION as LONGDESCR, MAC from PC_LIST WHERE active = true order by sissi, ip"; //use & instead of + to have blankspace instead of null values for displaying PCs without Sissi because CONCAT doesn't work (MS Access....)
OleDbDataAdapter da = new OleDbDataAdapter();
DataSet ds = new DataSet();
vcon.Open();
da.SelectCommand = new OleDbCommand(q, vcon);
vcon.Close();
da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
da.Fill(ds, "tblPCs");
DataTable dtPC = ds.Tables["tblPCs"];
foreach (DataRow dtRow in dtPC.Rows)
{
lstPCs.Items.Add(new KeyValuePair<String, String>(dtRow["LONGDESCR"].ToString(), dtRow["MAC"].ToString()));
}
lstPCs.DisplayMember = "Key";
lstPCs.ValueMember = "Value";
}
//.........................................
private void Dataset_get(string mymac)
{
string qp = "";
switch (mypermissions)
{
case "ADMIN":
qp = "SELECT USER_TYPE, HAS_ADMIN, USER_NAME, PASSWORD FROM PASSWORDS WHERE ID = '" + mymac + "' ORDER BY user_type";break;
case "USER":
qp = "SELECT p.USER_TYPE, p.HAS_ADMIN, p.USER_NAME, p.PASSWORD FROM PASSWORDS p, PC_LIST pc WHERE p.ID = '" + mymac + "' and p.ID = pc.MAC and (pc.x_plant like '%USER%' or (ucase(p.user_type) not like '%ADMIN%')) ORDER BY p.user_type";break;
default: break;
}
OleDbDataAdapter dapass = new OleDbDataAdapter();
DataSet dspass = new DataSet();
vcon.Open();
dapass.SelectCommand = new OleDbCommand(qp, vcon);
vcon.Close();
dapass.MissingSchemaAction = MissingSchemaAction.AddWithKey;
dapass.Fill(dspass,"tblPass");
DataTable dtPass = new DataTable();
dtPass = dspass.Tables["tblPass"];
dataGridView1.DataSource = dtPass;
}
//.........................................
void ListBox1SelectedIndexChanged(object sender, EventArgs e)
{
groupBox1.Text = lstPCs.GetItemText(lstPCs.SelectedItem);
string x = lstPCs.GetItemValue(lstPCs.SelectedItem).ToString();
Dataset_get(x);
}
OleDbConnection vcon 已共享。