Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
1.06/5 (3 votes)
See more:
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 ?
Posted
Updated 29-Dec-15 16:43pm
v2
Comments
Raje_ 29-Dec-15 7:52am    
Do you mean nested data grid view?
Member 12197595 29-Dec-15 23:48pm    
No sir..

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
 
Share this answer
 
Comments
Member 12197595 29-Dec-15 22:40pm    
Thank you sir, But I want user type in first column then corresponding data will shown in second column automatically(ie.register number and name). then user type some details in third column like school code then I want school name automatically display in fourth columnn
Ashutosh shukla207 30-Dec-15 0:01am    
on the grid cell event ... there is only one way to do the thing whatever u needed. take s combobox column before this column. u have to specify that what field of database u are going to enter . and check condition on cell event . than search and bind update datatable row . and bind again to the datagridview
Member 12197595 30-Dec-15 23:15pm    
Thank You sir."CellValueChanged" event works Properly.. :)
But it need to click to display answer. whether any event exist when we type data the matching results to be display ?
Member 12197595 30-Dec-15 23:21pm    
My code
private void dgv_student_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
Sp_student cuss = new Sp_student();

try
{
foreach (DataGridViewRow row in dgv_student.Rows)
{

if (!row.IsNewRow)
{

k = dgv_student.Rows[e.RowIndex].Cells[1].Value.ToString();
if (k != "")
{
dgv_student.Rows[e.RowIndex].Cells[2].Value = cuss.namefind(k);
}
if (dgv_student.Rows[e.RowIndex].Cells[3].Value != null)
{
string k=dgv_student.Rows[e.RowIndex].Cells[3].Value;
dgv_student.Rows[e.RowIndex].Cells[4].Value = cuss.findschool(k);

}

}
}

}

catch { }
}
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.

C#
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;
                    }
                }               
            }
        }
    }
}
 
Share this answer
 
Comments
Ashutosh shukla207 30-Dec-15 12:16pm    
its good ... but try to integrate the column where u have to enter values .
Member 12197595 30-Dec-15 23:22pm    
Thank you sir. My work is this.. is it correct?

private void dgv_student_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
Sp_student cuss = new Sp_student();

try
{
foreach (DataGridViewRow row in dgv_student.Rows)
{

if (!row.IsNewRow)
{

k = dgv_student.Rows[e.RowIndex].Cells[1].Value.ToString();
if (k != "")
{
dgv_student.Rows[e.RowIndex].Cells[2].Value = cuss.namefind(k);
}
if (dgv_student.Rows[e.RowIndex].Cells[3].Value != null)
{
string k=dgv_student.Rows[e.RowIndex].Cells[3].Value;
dgv_student.Rows[e.RowIndex].Cells[4].Value = cuss.findschool(k);

}

}
}

}

catch { }
}
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900