尝试检测已填充的组合 [英] Trying to detect combo populated

查看:39
本文介绍了尝试检测已填充的组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已要求我解决现有.NET表单代码的问题.有一个组合框,其中填充了数据库中的服务器名称.当用户从组合中选择其他服务器时,新的服务器ID号将保存到参数"表中的字段中,以将其记录为最后选择的服务器.这样,第二天启动应用程序时,它始终显示与上次使用该应用程序相同的服务器.

这一直有效,直到选择了列表中的第一个条目.我的直觉是,这是由于索引为0,并且确定SelectedIndexChanged中的代码包含...

I''ve been asked to fix an issue with existing .NET forms code. There''s a combo box populated with server names from a database. When the user selects a different server from the combo, the new server ID number is saved into a field in the ''params'' table to record it as the last server selected. That way, when the app is started up the next day, it always displays the same server as when the app was last used.

This worked until the very first entry in the list was selected. My hunch was that this would be due to the index being 0 and sure enough the code in SelectedIndexChanged contains...

if (Convert.ToInt32(cmbServer.SelectedIndex) > 0)
{
   int iServerID = Convert.ToInt32(cmbServer.SelectedValue.ToString());
   PlattyData.SaveDefaultServer(iServerID);
}



SelectedIndex在初始化时设置为-1,因此我将IF行更改为:



The SelectedIndex is set on initialisation to -1 so I changed the IF line to :

if (Convert.ToInt32(cmbServer.SelectedIndex) > -1)



但是,当selectedindex = 0时,cmbServer.SelectedValue.ToString())给出错误消息输入字符串的值不正确"

因此,基本上如代码所示,除了第一个选择的服务器之外,其他任何服务器都可以正常工作,但是第一个服务器(索引为0)会导致错误.

任何更适当的检查方法将不胜感激!

编辑

cmbServer.SelectedValue正常工作时的内容是ServerID号,例如23.失败时,cmbServer.SelectedValue ="System.Data.DataView"

填充代码为:



However, when the selectedindex = 0 the cmbServer.SelectedValue.ToString()) gives an error of "Input string was not in a correct value"

So basically as the code stands, any server selected other than the first works fine, but the first server (with an index of 0) causes an error.

Any ideas on a more appropriate check would be greatly appreciated!!

EDIT

The contents of cmbServer.SelectedValue when it''s working properly is the ServerID number eg 23. When it fails, cmbServer.SelectedValue = "System.Data.DataView"

The fill code is :

private void SetupServerCombo()
{
    // 1 Fill Combo
    DataTable dt = new DataTable();

    dt = PlattyData.GetServers();
    cmbServer.SelectedIndex = -1;
    cmbServer.DataSource = dt;
    cmbServer.DisplayMember = "ServerName";
    cmbServer.ValueMember = "ServerID";

    // 2 Get default server
    int iDefaultServerID = -1;
    iDefaultServerID = PlattyData.GetDefaultServerID();

    cmbServer.SelectedValue = iDefaultServerID;
}

推荐答案

您的问题在这里:
Your problem is here:
cmbServer.SelectedValue = iDefaultServerID;


这不是预选"项目的方法.
尝试以下方法:


This is not the way to ''preselect'' an item.
Try this instead:

if (cmbServer.Items.IndexOf(iDefaultServerID) > -1)
    cmbServer.SelectedIndex = cmbServer.FindStringExact(iDefaultServerID.ToString());
else
    cmbServer.SelectedIndex = 0; // if serverId not found, default to first entry 




这可能是您的问题:
第一次要通过此代码设置组合时
cmbServer.SelectedValue = iDefaultServerID;

您的组合中未定义ideFaultServerID!所以它不能设置正确!

也是这样




this might be your problem:
At the first time when you want to set your combo by this code
cmbServer.SelectedValue = iDefaultServerID;

the ideFaultServerID is not defined in your combo! so It can not set it correct!

So do this


int index=dt.Columns["ServerID"].Ordinal;
bool found=false;
  for (int i = 0; i < comboBox1.Items.Count; i++)
{
 if (((DataRow) comboBox1.Items[i]).ItemArray[i]==iDefaultServerID)
 {
   faound=true;
   comboBox1.selectedIndex=i;
   break;
 }
}
if (!found)    comboBox1.selectedIndex=0;



如果可行,您可能需要改进此代码!



if it works, you may wnat to improve this code!


我可能会看到问题:

I may see the problem:

if (Convert.ToInt32(cmbServer.SelectedIndex) > 0)
{
   int iServerID = Convert.ToInt32(cmbServer.SelectedValue.ToString());
   PlattyData.SaveDefaultServer(iServerID);
}



也许Convert.ToInt32(cmbServer.SelectedIndex) > 0应该是Convert.ToInt32(cmbServer.SelectedIndex) >= 0



Maybe the Convert.ToInt32(cmbServer.SelectedIndex) > 0 should be Convert.ToInt32(cmbServer.SelectedIndex) >= 0


这篇关于尝试检测已填充的组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆