C# DataGridView显示数组

来源:互联网 发布:zhao家人是什么 知乎 编辑:程序博客网 时间:2024/05/01 09:59
          private void Form1_Load(object sender, EventArgs e)

        {

            //第一种做法(不如人意,没有达到要求,只显示字符串的长度)

            //string[] intarray =new string[] {"one","two","three"};
            //dataGridView1.DataSource = intarray;//使用字符串数组,结果显示字符串的第一个公共属性Length


            //第二种做法
            Item[] items = new Item[]                                               
            { 
                new Item("one"),
                new Item("two"), 

                new Item("three")

            };

            this.dataGridView1.DataSource = items;
        }


        class Item

        {

            private string txt;

            public string Text
            {
                get { return txt; }
            }


            public Item(string text)
            {
                this.txt = text;
            }

        }

         //另附以下程序
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {

            if (e.RowIndex < 0)    //当表头选中时
            {
                return;
            }

            if (dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString() == "two")           //当选中“two”时,checkBox1被选中
            {
                checkBox1.Checked = true;
            }
            else
            {

                checkBox1.Checked = false;

            }

        }

//第一种做法运行结果如下:

//第二种做法行结果如下:

//当选中“two”时,checkBox1被选中



DataGridView 控件使用方法可参考:

http://blog.csdn.net/weishuchan/article/details/7981726

http://blog.sina.com.cn/s/blog_4935bf700100mtv7.html

原创粉丝点击