我怀疑,如何在c#.net中填充一个下拉列表框详细信息 [英] I have doubt, how to populate one dropdown list box details to other in c#.net

查看:48
本文介绍了我怀疑,如何在c#.net中填充一个下拉列表框详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有疑问,如何在没有SQL命令的情况下将一个下拉列表框详细信息填充到其他C#.net





< b>我尝试了什么:



我试图生成但没有用,

ddcar.Items.Add(选择你的车);

ddcar.Items.Add(MARUTHI);

ddcar.Items.Add(FORD);



ddmodel.Items.Add(800);

ddmodel.Items.Add(OMNI);



如何填充

i have doubt, how to populate one dropdown list box details to other in C#.net
without SQL commands.

What I have tried:

I tried to generate but no use,
ddcar.Items.Add("Select Your Car");
ddcar.Items.Add("MARUTHI");
ddcar.Items.Add("FORD");

ddmodel.Items.Add("800");
ddmodel.Items.Add("OMNI");

how to populate

推荐答案

朋友们谢谢。



终于得到了解决方案=但是使用SQL连接;

---------------------

使用系统;

使用System.Collections.Generic;

使用System.Linq;

使用System.Web;

使用System.Web.UI;

使用System.Web.UI.WebControls;

使用System.Data.SqlClient;

使用System.Data;

使用System.Web.Configuration;





public partial class Default2:System.Web.UI.Page

{

protected void Page_Load(object发件人,EventArgs e)

{

if(!Page.IsPostBack)

{

string str = WebConfigurationManager .ConnectionStrings [conn]。ConnectionString;

SqlConnection cn = new SqlConnection(str);

SqlCommand cmd = new SqlCommand();

cmd.CommandText =从汽车中选择carid;

cmd.Connection = cn;

SqlDataAdapter ada = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();

ada.Fill(ds,car);

DropDownList1.DataSource = ds.Tables [0] .DefaultView;

DropDownList1.DataValueField = ds.Tables [0] .Columns [carid]。ToString();

DropDownList1.DataBind();





}

}

protected void DropDownList1_SelectedIndexChanged(object sender,EventArgs e)

{

string str = WebConfigurationManager。 ConnectionStrings [conn]。ConnectionString;

SqlConnection cn = new SqlConnection(str);

SqlCommand cmd = new SqlCommand();

cmd.CommandText =从car中选择cardetail carid ='+ DropDownList1.SelectedItem.ToString()+';



cmd.Connection = cn;

cn.Open();

SqlDataReader博士;

dr = cmd.ExecuteReader();

if( dr.Read())

{

TextBox1.Text = dr [0] .ToString();





}

dr.Close();

cn.Close();
Friends Thanks.

Finally Got solution = but with SQL connection;
---------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Web.Configuration;


public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string str = WebConfigurationManager.ConnectionStrings["conn"].ConnectionString;
SqlConnection cn = new SqlConnection(str);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Select carid from car";
cmd.Connection = cn;
SqlDataAdapter ada = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
ada.Fill(ds,"car");
DropDownList1.DataSource = ds.Tables[0].DefaultView;
DropDownList1.DataValueField = ds.Tables[0].Columns["carid"].ToString();
DropDownList1.DataBind();


}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string str = WebConfigurationManager.ConnectionStrings["conn"].ConnectionString;
SqlConnection cn = new SqlConnection(str);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Select cardetail from car where carid='" + DropDownList1.SelectedItem.ToString() + "'";

cmd.Connection = cn;
cn.Open();
SqlDataReader dr;
dr = cmd.ExecuteReader();
if (dr.Read())
{
TextBox1.Text = dr[0].ToString();


}
dr.Close();
cn.Close();


嗨Sermaraj,



如果我清楚地理解你的版本那么你想要f生成使用内部列表集合的第一个下拉框(不使用任何数据库表和SQL命令)

然后您希望根据第一个列表框的选择过滤第二个下拉框但是也应该在内部管理C#代码(不使用SQL命令)



如果这是真的那么这就是你的解决方案



Hi Sermaraj,

If I have understood your quersion clearly then you want to fill the first dropdown box using internal list collection (without using any database table and SQL command)
then you want second dropdown box to be filtered based on selection of first list box but that too should manage internally withing C# code (without using using SQL command)

so if this is true then here is your solution

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace _26196_filtering_collections_in_c_sharp
{
    public partial class Form1 : Form
    {
        public static List<CarModel> lstCarModel = new List<CarModel>();
        public Form1()
        {
            InitializeComponent();

            lstCarModel.Add(new CarModel("MARUTHI", "800"));
            lstCarModel.Add(new CarModel("FORD", "FIGO"));

            this.comboBox1.DataSource = lstCarModel;
            this.comboBox1.DisplayMember = "Car";
            this.comboBox1.ValueMember = "Car";
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            List<CarModel> FilteredCarModel = lstCarModel;
            var filtered = from CarModel cm in lstCarModel
                           where cm.Car == comboBox1.Text.ToString()
                           select cm;
            this.comboBox2.Items.Clear();
    

            foreach (CarModel carmodel in filtered)
            {
                this.comboBox2.Items.Add(carmodel.Model.ToString());
            }
           
        }

        public class CarModel
        {
            string car;
            public string Car{get { return car; }}
            string model;
            public string Model{get { return model; }}
            public CarModel(string car, string model){this.car= car;this.model= model;}
            public override string ToString(){return string.Format("{0}: {1}", car, model);}
        }
    }
}


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Person p1 = new Person { Id = "A", Name = "Kayes", Age = 29, JoiningDate = DateTime.Parse("2010-06-06") };
        Person p2 = new Person { Id = "B", Name = "Gibbs", Age = 34, JoiningDate = DateTime.Parse("2008-04-23") };
        Person p3 = new Person { Id = "C", Name = "Steyn", Age = 28, JoiningDate = DateTime.Parse("2011-02-17") };

        List<person> persons = new List<person>();
        persons.Add(p1);
        persons.Add(p2);
        persons.Add(p3);

       ExampleDDL.DataTextField = "Name";
        ExampleDDL.DataValueField = "Id";

        ExampleDDL.DataSource = persons;
        ExampleDDL.DataBind();
    }
}

public class Person
{
    public string Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
    public DateTime JoiningDate { get; set; }
}

这是一个例子。我相信它也适用于WPF或Windows Forms。仅供参考,ExampleDDL是网页上定义的下拉列表。

Here is an example. I believe it can also work in WPF or Windows Forms. FYI, ExampleDDL is the dropdown list defined on the web page.


这篇关于我怀疑,如何在c#.net中填充一个下拉列表框详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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