C#中 DataGirdView 控件的查询功能

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhao3132453/article/details/87967102

需求如下:

查询poly的年龄

示例代码:

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;

namespace DataTable_Match
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //添加列
            for (int i = 1; i <= 5; i++)
            {
                dataGridView1.Columns.Add("", "test" + i.ToString());
            }

            //添加行
            for (int j = 1; j <= 5; j++)
            {
                dataGridView1.Rows.Add("row1" + j.ToString(), "row2" + j.ToString(), "row3" + j.ToString(), "row4" + j.ToString(), "row5" + j.ToString());
            }

            //查找 row11 对应的 第二列数据(同行)
            string strSource = "row11";

            foreach (DataGridViewRow dr in dataGridView1.Rows)
            {
                if(dr.Cells[0].Value != null)
                {
                    if (dr.Cells[0].Value.ToString() == strSource)
                    {
                        string strDest = dr.Cells[1].Value.ToString();
                    }
                }               
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/zhao3132453/article/details/87967102