在列表视图中显示arralist [英] Display arralist in listview

查看:66
本文介绍了在列表视图中显示arralist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hallo我在项目中添加了2个类,使用数组列表,我想在窗体上的列表框中显示这些数组列表,这是我的类


Hallo I have added 2 classes to my project witch use array list i would like to display those array lists in an listbox on my form here are my classes


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.Sql;
using System.Collections;
using System.Data.SqlTypes;

namespace SqlServer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
           public class SqlServerList : IComparable, ICloneable
        {
            public SqlServerList()
            {
                ServerName = string.Empty;
                InstanceName = string.Empty;
                IsClustered = string.Empty;
                Version = string.Empty;
            }

            #region ICloneable Members

            public object Clone()
            {
                try
                {
                    if (this == null)
                    {
                        return null;
                    }
                    SqlServerList SqlSL = new SqlServerList { ServerName = ServerName, InstanceName = InstanceName, IsClustered = IsClustered, Version = Version };
                    return SqlSL;
                }
                catch
                {
                    throw new NotImplementedException();
                }
            }

            #endregion
           
            #region IComparable Members

            public int CompareTo(object obj)
            {
                try
                {
                    if (!(obj is SqlServerList))
                    {
                        throw new Exception("obj is not an instance of SqlServerList");
                    }
                    if (this == null)
                    {
                        return -1;
                    }
                    return ServerName.CompareTo((obj as SqlServerList).ServerName);
                }
                catch
                {
                    throw new NotImplementedException();
                }
            }

            #endregion

            public string ServerName { get; set; }
            public string InstanceName { get; set; }
            public string IsClustered { get; set; }
            public string Version { get; set; }
        }
         public class GetInstance
        {
            public static ArrayList GetInstanceName()
            {
                try
                {
                    SqlServerList SqlSL = new SqlServerList();
                    SqlDataSourceEnumerator instance = SqlDataSourceEnumerator.Instance;
                    DataTable table = instance.GetDataSources();
                    ArrayList list = new ArrayList();
                    foreach (DataRow row in table.Rows)
                    {
                        SqlSL = new SqlServerList();
                        SqlSL.ServerName = row[0].ToString();
                        SqlSL.InstanceName = row[1].ToString();
                        SqlSL.IsClustered = row[2].ToString();
                        SqlSL.Version = row[3].ToString();
                        list.Add(SqlSL);
                        
                    }
                    return list;
                }
                catch
                {
                    return null;
                }

            }

        }

        private void button1_Click(object sender, EventArgs e)
        {

            listBox1.DataSource = GetInstance.GetInstanceName();        
            

        }
    }
}

推荐答案

为什么创建了两个相同的类?
Why your created two identical classes?
listBox.DataSource = GetInstance.GetInstanceName();
listBox.DataMember = "ServerName";



希望它能以任何方式提供帮助.



Hope it helps in any way.


这篇关于在列表视图中显示arralist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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