如何在Datagridview中进行动态输入和选择 [英] How Can I Do Dynamic Entering And Selection In A Datagridview

查看:55
本文介绍了如何在Datagridview中进行动态输入和选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生,

先生现在我正在使用数据网格视图。在我想要的数据网格视图中,当我输入数据时(例如,像寄存器号),然后我想在下一个数据网格视图单元格中显示相应的学生名称,然后用户在第三列中输入一些细节,如学校代码然后我希望学校名称自动显示在第四栏。我怎样才能做到这一点 ??请帮帮我..我用哪个活动?如何获取当前单元格值?

hi sir,
sir Now I am working with a data grid view. In this data grid view I want to make, when I enter a data (e.g.. like a register no) then I want to show the corresponding student name in next data grid view cell,then user type some details in third column like school code then I want school name automatically display in fourth column. How can I do this ?? Please help me .. Which event I use ? How can I get the current cell value ?

推荐答案

您好,



您可以使用datagridview CellValueChanged事件,每次触发都会修改列中的单元格值。



以下是MSDN



祝你好运,

Sathish
Hi,

You can use the datagridview "CellValueChanged" event, which fires each time modify the cell value in column.

Here is the example from MSDN

Best regards,
Sathish


您好,



我添加了winform应用程序示例代码,该代码有四列。第一列接受学生ID,然后在下一列中获取名称。第二栏接受在另一栏中取得学校名称的学号。



Hi,

I have added the winform application example code, which has four columns. First column accept the student id then fetch the name in next column. Second column accept the school number which fetch the school name in another column.

using System;
using System.Collections.Generic;
using System.Windows.Forms;

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

        Dictionary<int, string> studentCollection = new Dictionary<int, string>
        {
            [001] = "S1",
            [002] = "S2",
            [003] = "S3",
            [004] = "S4",
        };

        Dictionary<int, string> schoolCollection = new Dictionary<int, string>
        {
            [011] = "S1",
            [022] = "S2",
            [033] = "S3",
            [044] = "S4",
        };

        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
            {
                var cellValue = Convert.ToString(this.dataGridView1[e.ColumnIndex, e.RowIndex].Value);
                int keyValue;
                if(int.TryParse(cellValue, out keyValue))
                {
                    if (e.ColumnIndex == 0)
                    {
                        var student = studentCollection[keyValue];
                        this.dataGridView1[1, e.RowIndex].Value = student;
                    }
                    else if (e.ColumnIndex == 2)
                    {
                        var school = schoolCollection[keyValue];
                        this.dataGridView1[3, e.RowIndex].Value = school;
                    }
                }               
            }
        }
    }
}


您好,



使用网格视图GridView_SelectedIndexChanged事件,并使用选定的行ID并将此id传递给数据库并根据用户ID获取用户详细信息,现在使用此下一个网格视图绑定数据并对学校名称使用相同的方法。
Hello,

Use grid view GridView_SelectedIndexChanged event, and use selected row ID and pass this id to database and fetch user details according to user id, now bound data with this next grid view and use this approach same for school name.


这篇关于如何在Datagridview中进行动态输入和选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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