下拉绑定问题-C# [英] Dropdown Binding issue - C#

查看:82
本文介绍了下拉绑定问题-C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经从sql查询中填充了一个数据集,并且该数据集返回了以下值.

ID CID
3 7
8466
23566
11 700

现在,我想修改CID,然后将ID和CID与Dropdown绑定.下拉菜单的Datavaluefield将是以下ID,而Datatextfield将是CID以下.

ID CID
3 C0007
8 C0466
23 C0566
11 C0700

我已经写了下面的代码,但是它不能正常工作,因为请检查循环可能是循环中的一些问题.请交叉检查以下代码并尽快提供帮助.

Hi,

I have filled one dataset from sql query and that dataset returns the below values.

ID CID
3 7
8 466
23 566
11 700

Now I want to modify CID and after that ID and CID should be bind with Dropdown. Datavaluefield of dropdown would be the below ID''s and Datatextfield would be below CID''s.

ID CID
3 C0007
8 C0466
23 C0566
11 C0700

I have written the below code but it is not working fine as please check loop may be some issue in loop. Please cross check the below code and help asap.

for (int i = 0; i < dset.Tables["Table"].Rows.Count; i++)
                {
                    foreach (DataRow myRow in dset.Tables["Table"].Rows)
                    {
                        int CCid;
                        CCid = (int)myRow["CID"];

                        string str = String.Format("C{0:0000}", CCid);

                        DataTable table = new DataTable("MyTable");

                        table.Columns.Add("id", typeof(int));
                        table.Columns.Add("name", typeof(string));

                        string[] names = { str };

                        DataRow dr;

                        for (int j = 0; j < dset.Tables["Table"].Rows.Count; j++)
                        {
                            dr = table.NewRow();

                            dr["id"] = j + 1;

                            dr["name"] = Convert.ToString(names[str[0]]);

                            table.Rows.Add(dr);
                        }

                        DataSet ds = new DataSet();

                        ds.Tables.Add(table);

                        ddlContract.DataSource = ds.Tables["MyTable"];
                        ddlContract.DataTextField = "name";
                        ddlContract.DataValueField = "id";
                        ddlContract.DataBind();
                    }
                }

推荐答案

您在使用comboBox吗?如果是这样,请尝试以下代码..

Hi are you using the comboBox? If so, try the following code..

ComboBox1.DisplayMember = "name";
ComboBox1.ValueMember = "id";
ComboBox1.DataSource =  ds.Tables["MyTable"];
ComboBox1.Databind();


为什么需要三个循环?循环中存在循环,这会给您造成重大问题...

Why do you need three loops? You have loops within loops which is creating major problems for you...

DataTable table = new DataTable("MyTable");
DataRow dr;
table.Columns.Add("id", typeof(int));
table.Columns.Add("name", typeof(string));
 

foreach (DataRow myRow in dset.Tables["Table"].Rows)
                    {
                        int CCid;
                        CCid = (int)myRow["CID"];
 
                        string str = String.Format("C{0:0000}", CCid);
                        string[] names = { str };
                        dr = table.NewRow();
                        dr["id"] = j + 1;
                        dr["name"] = Convert.ToString(names[str[0]]);
                        table.Rows.Add(dr);
                        }
 
                 ds.Tables.Add(table);
                 ddlContract.DataSource = ds.Tables["MyTable"];
                 ddlContract.DataTextField = "name";
                 ddlContract.DataValueField = "id";
                 ddlContract.DataBind();



仍然离有效代码不远!!但是出于教育目的,没关系.



Still it''s no way near effecient code!! but for educational purposes it''s ok..


这篇关于下拉绑定问题-C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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