In this article basically we are going to learn how to create a link button in a DataGridView and on clicking that button the values for a row are transferred to another form. This scenario can be best suited in the case of training institutes. Let's suppose in training institutes that what happens is a student comes for an enquiry and all the details of the student are entered into the Enquiry table. After the enquiry is done either the student does or does not take an admission. If he/she takes the admission then we need to add the details to the admission table so instead of adding it manually we can create a link in a DataGridView in the EnquiryForm.cs which will copy the details of the student to the AdmissionForm.cs on a single click of the link and on that form we'll just click the button and the work is done.
So for doing this activity we need to create an enquiry table in the SQL Server. I'm making a table like this:
create table Enquiry
(
EnquiryId int identity(100,1)not null,
vName varchar(20) not null,
vAdd varchar(20) not null,
vQualification varchar(20) not null,
vCourseInterested varchar(20) not null,
constraint enqid primary key(EnquiryId)
)
Now insert 5 records into the Enquiry Table:
insert into Enquiry values('Vishal','Andheri','MCA','Software Eng')
insert into Enquiry values('Lincy','Worli','BCA','PHP')
insert into Enquiry values('Pinky','Khar Rd','Bsc.IT','Hardware Eng')
insert into Enquiry values('Kailash','Santacruz','Msc.IT','Java')
insert into Enquiry values('Jack','Virar','MCA','.NET')
We are not ing values for the enquiryId because it is an identity column so SQL Server will automatically insert a value for it; what we did specifiy was a seed and increment clause of the identity clause.
We also need to create a table Admission in SQL Server:
create table Admission
(
Enquiryid int not null,
AdmissionId int identity(100,1)not null,
vName varchar(20) not null,
vAdd varchar(20) not null,
vQualification varchar(20) not null,
vCourseInterested varchar(20) not null,
constraint pkAdmId primary key(AdmissionId),
constraint fkenqid foreign key(EnquiryId) references Enquiry(EnquiryId)
)
Now open your Visual Studio and create a new Windows Form.
Add a DataGridView control on the Windows Form and set the dock property to top.
Now it's time to create the link on the DataGridView.
- Right click on the DataGridView and click on AddColumn from the list.
- When you click you'll get a AddColumn Dialog box.
- Specify the name as AddLink.
- For the Type select DataGridViewLinkColumn from the Type drop down box.
- For the Header text use Add to Addmission.
- Now press the Add button to add the column and then click the Close button to close the Add column dialog box.
- Your Form now has the Add to Addmission column.
- Again right click on the DataGridView and now click edit column link.
- An edit column dialog box appears.
- Specify the Text property to Add and useColumnTextforLink to true from the property of the Add to Admission Link.
- Now press ok.
Now add one more form to your application and name it AdmissionForm.cs
And design it like this.
Set the text property of textbox1 to read only, so that no one can change it.
Now let's come to the coding part. Open your EnquiryForm.cs and add the code to display data in the DataGridView.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace EnqAddForm
{
public partial class Form1 : Form
{
SqlConnection con;
SqlDataAdapter da;
DataSet ds;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
con = new SqlConnection("Data Source=D-NIIT-CR02;Initial Catalog=AdventureWorks;User Id=sa;word=faculty");
da = new SqlDataAdapter("Select * from Enquiry", con);
ds = new DataSet();
da.Fill(ds, "Enquiry");
dataGridView1.DataSource = ds.Tables["Enquiry"].DefaultView ;
}
}
}
Now to check which link was clicked on the DataGridView generate the default event for the DataGridView by double-clicking on it.
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
foreach (DataGridViewRow dr in dataGridView1.Rows)
{
if (dr.Cells[0].Selected == true)
{
/*Now here there are two ways to data to admission form.
either you data through constructor or you make all textbox modifier property to public.
so it can accessed over here by using the object of AdmissionForm.
For eg : obj.textBox1.Text=value;
Over here i'm using the first method to data ie through constructor. over here we have to
make some changes in the AdmissionForm constructor.
* */
int id =Convert.ToInt32(dr.Cells[1].Value);
string name = dr.Cells[2].Value.ToString();
string add = dr.Cells[3].Value.ToString();
string qualification = dr.Cells[4].Value.ToString();
string course = dr.Cells[5].Value.ToString();
AdmissionForm obj = new AdmissionForm(id,name,add,qualification,course);
obj.Show();
}
}
}
Now make some changes in the AdmissionForm.cs.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace EnqAddForm
{
public partial class AdmissionForm : Form
{
int enqid;
string stdname, stdadd, stdqualification, stdcourse;
public AdmissionForm(int id,string name,string add,string quali,string course)
{
InitializeComponent();
enqid = id;
stdname = name;
stdadd = add;
stdqualification = quali;
stdcourse = course;
}
private void AdmissionForm_Load(object sender, EventArgs e)
{
textBox1.Text = enqid.ToString();
textBox2.Text = stdname;
textBox3.Text = stdadd;
textBox4.Text = stdqualification;
textBox5.Text = stdcourse;
}
private void button1_Click(object sender, EventArgs e)
{
//Write the code for adding the data to Admission table.
con = new SqlConnection("Data Source=D-NIIT-CR02;Initial Catalog=AdventureWorks;User Id=sa;word=faculty");
con.Open();
cmd = new SqlCommand("Insert into Admission values(@enqid,@name,@add,@quali,@course)", con);
cmd.Parameters.AddWithValue("@enqid", enqid);
cmd.Parameters.AddWithValue("@name", stdname);
cmd.Parameters.AddWithValue("@add", stdadd);
cmd.Parameters.AddWithValue("@quali", stdqualification);
cmd.Parameters.AddWithValue("@course", stdcourse);
int rows = cmd.ExecuteNonQuery();
if (rows > 0)
{
MessageBox.Show("Inserted Successfully");
}
else
{
MessageBox.Show("Error Inserting data");
}
}
}
}
Now you can run the application to test the functionality when you debug the application your form will display all the details from the Enquiry table in the DataGridView with a link in the first cell.
Click on any of the Add link buttons; the data for the link you clicked will appear in the Admission form.cs form. The only thing left for you to do is to write the code to add data in the Admission table.
Hope this example helps you to carry out the project.
Untill that time enjoy programming.