65 838
Assignments Done
99,1%
Successfully Done
In October 2018

Answer to Question #27921 in C# for vijay

Question #27921
there are two gridview one grid view contains three columns(id,firstname,lastname) and 15 rows and another has four columns(id,firstname,lastname,password) 45 rows. nw when i click row which has id 1 in 1st gridview ,i need details belongings to id 1 in second gridview.


in c# codings
Expert's answer
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace Question__27921
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
private& DataTable _dtFirst;
private DataTable _dtLast;

public MainForm()
{
InitializeComponent();
Initialize_FirstTable();
Initialize_LastTable();
_bsFirst.DataSource = _dtFirst;
}

void MainFormLoad(object sender, EventArgs e)
{
//LoadDataTo first table
AddDataFirst("Peter", "Perry");
AddDataFirst("Michael", "Powell");
AddDataFirst("Robert", "Holmes");
AddDataFirst("Logan", "York");
AddDataFirst("Grace", "Walton");
AddDataFirst("Carol", "Richard");
AddDataFirst("Lily", "Tate");
AddDataFirst("Joshua", "McDowell");
AddDataFirst("Harry", "Peters");
AddDataFirst("Susan", "Hubbard");
AddDataFirst("Noreen", "Price");
AddDataFirst("Michael", "Harmon");
AddDataFirst("Chastity", "Ramsey");
AddDataFirst("Kenneth", "Hunter");
AddDataFirst("Madison", "French");
//Load data to last table
AddDataLast("Ketty", "Hunter", "73NlF_7");
AddDataLast("Met", "Ramsey", "a#t^M8");
AddDataLast("Peter", "Mills", "1q2w3e4r");
AddDataLast("Peter", "Hunter", "1q2#4e4r");
AddDataLast("Michael", "Mills", "vf%rEte");
AddDataLast("Robert", "York", "1An312qw");
AddDataLast("Oswald", "York", "rMg_bu7&d");
AddDataLast("Henry", "Hubbard", "gRenndo");
AddDataLast("Edward", "Richard", "A5fZ8_9");
AddDataLast("Christopher", "Lang", "fW4_o0n");
AddDataLast("Peter", "Perry", "1q2w3e4r");
AddDataLast("Michael", "Powell", "vf%rrte");
AddDataLast("Robert", "Holmes", "12312qw");
AddDataLast("Logan", "York", "r5g_bu7&d");
AddDataLast("Grace", "Walton", "gtnndo");
AddDataLast("Carol", "Richard", "A3fZ8_9");
AddDataLast("Susan", "Hubbard", "95g7Bk");
AddDataLast("Noreen", "Price", "0(mUf%%");
AddDataLast("Michael", "Harmon", "7E4n^gg");
AddDataLast("Chastity", "Ramsey", "py*nR55");
AddDataLast("Kenneth", "Hunter", "73NlF_7");
AddDataLast("Simon", "Richard", "A3fZ8_9");
AddDataLast("Paul", "Richard", "vf%rrte");
AddDataLast("Robert", "Hunter", "12312qw");
AddDataLast("Richard", "York", "r5g_bu7&d");
AddDataLast("Tom", "Walton", "gtnndo");
AddDataLast("Helen", "Ball", "f$_o0n");
AddDataLast("Joshua", "Prisby", "M6n_fo");
AddDataLast("Madison", "French", "a#t^M8");
AddDataLast("Bob", "Peterson", "0_i7Gd%");
AddDataLast("Diane", "Hubbard", "95g7Bk");
AddDataLast("Susan", "Price", "0(mUf%%");
AddDataLast("Michael", "Ball", "7E4n^gg");
AddDataLast("Chastity", "Walton", "py*nR55");
AddDataLast("Lily", "Tate", "f$_o0n");
AddDataLast("Joshua", "McDowell", "M6n_fo");
AddDataLast("Harry", "Peters", "0_i7Gd%");
AddDataLast("Jordan", "Hampton", "M90n_fo");
AddDataLast("Erick", "Peters", "0_i7Gd%");
AddDataLast("Lily", "Hubbard", "95g7Bk");
AddDataLast("Noreen", "Richard", "0(mUf%%");
AddDataLast("Nick", "Harmon", "7E4n^gg");
AddDataLast("Harry", "Ramsey", "py*nR55");
AddDataLast("Kenneth", "Ball", "73NlF_7");
AddDataLast("Madison", "Scott", "a#t^M8");
dgFirst.Refresh();
dgLast.Refresh();
}

/// <summary>
/// Initialization DataTable First
/// </summary>
private void Initialize_FirstTable()
{
_dtFirst = new DataTable("First");
DataColumn column;
column = new DataColumn
{
DataType = Type.GetType("System.Int32"),
ColumnName = "id",
AllowDBNull = false,
AutoIncrement = true,
AutoIncrementSeed = 1,
AutoIncrementStep = 1,
ReadOnly = true,
Unique = true
};
_dtFirst.Columns.Add(column);
var pk = new DataColumn[1];
pk[0] = _dtFirst.Columns["id"];
_dtFirst.PrimaryKey = pk;

column = new DataColumn
{
DataType = Type.GetType("System.String"),
ColumnName = "FirstName"
};
_dtFirst.Columns.Add(column);

column = new DataColumn
{
DataType = Type.GetType("System.String"),
AllowDBNull = false,
ColumnName = "LastName"
};
_dtFirst.Columns.Add(column);
}

/// <summary>
/// Initialization DataTable First
/// </summary>
private void Initialize_LastTable()
{
_dtLast = new DataTable("Last");
DataColumn column;
column = new DataColumn
{
DataType = Type.GetType("System.Int32"),
ColumnName = "id",
AllowDBNull = false,
AutoIncrement = true,
AutoIncrementSeed = 1,
AutoIncrementStep = 1,
ReadOnly = true,
Unique = true
};
_dtLast.Columns.Add(column);
var pk = new DataColumn[1];
pk[0] = _dtLast.Columns["id"];
_dtLast.PrimaryKey = pk;

column = new DataColumn
{
DataType = Type.GetType("System.String"),
ColumnName = "FirstName"
};
_dtLast.Columns.Add(column);

column = new DataColumn
{
DataType = Type.GetType("System.String"),
AllowDBNull = false,
ColumnName = "LastName"
};
_dtLast.Columns.Add(column);

column = new DataColumn
{
DataType = Type.GetType("System.String"),
AllowDBNull = false,
ColumnName = "Password"
};
_dtLast.Columns.Add(column);
}

private void AddDataFirst(string firstName, string lastName)
{
var row = _dtFirst.NewRow();
row["FirstName"] = firstName;
row["LastName"] = lastName;
_dtFirst.Rows.Add(row);
}

private void AddDataLast(string firstName, string lastName, string password)
{
var row = _dtLast.NewRow();
row["FirstName"] = firstName;
row["LastName"] = lastName;
row["Password"] = password;
_dtLast.Rows.Add(row);
}

void DgFirstCellClick(object sender, DataGridViewCellEventArgs e)
{
if(e.RowIndex == -1) return;
var v = dgFirst.Rows[e.RowIndex].Cells["FirstName"].Value;
var vL = dgFirst.Rows[e.RowIndex].Cells["LastName"].Value;
_bsLast.DataSource = _dtLast;
_bsLast.Filter = string.Format("FirstName = '{0}' AND LastName = '{1}'", v, vL);
dgLast.Refresh();
}
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions