This article will show you how you can add a button control
inside the datagridview and retrieve the row value on click of button present
in the row of datagridview using c#.net in windows application.
Some of my previous articles are as follows: Paging
in DataGridview Using C#.Net In Windows Application, How
to Bind xml to DataGridView Using C#.Net In Windows Application, Bind
And Display Image in a DatagridView Using C#.Net in Windows Application, How
to Bind Data To TextBox Values in Asp.Net GridView Control Using C#.Net, How
To Make a Single Row of DataGridview Bold Using C#.Net in Windows Application,
Bind
DataGridView In Windows Application Using C#.
So for this article first we will create a new windows application and add a datagridview control in your form. Now click on edit column and add some rows now we will add a column of column type “DataGridViewButtonColumn”. Nowadd the text property and select true for “UseColumnTextForButtonValue”.
Have a look of this image
If you not do UseColumnTextForButtonValue=True, on that case text will not appear on button of datagridview.
Add the below code in your form code.
Add the below code in your form code.
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public
Form1()
{
InitializeComponent();
}
public
DataTable GetTable()
{
DataTable
dt = new DataTable();
dt.Columns.Add("Id", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Location", typeof(string));
dt.Columns.Add("Date", typeof(DateTime));
// Here
we add five DataRows.
dt.Rows.Add(1, "Vinay", "India",
DateTime.Now);
dt.Rows.Add(2, "Imran", "Pakistan",
DateTime.Now);
dt.Rows.Add(3, "Pawan", "U.S.",
DateTime.Now);
dt.Rows.Add(4, "Manash", "Nepal",
DateTime.Now);
dt.Rows.Add(5, "Ranu", "Sri
Lanka", DateTime.Now);
return dt;
}
private
void Form1_Load(object
sender, EventArgs e)
{
DataTable
dt = new DataTable();
dt = GetTable();
if
(dt.Rows.Count > 0)
{
dataGridView1.DataSource =
dt;
}
}
private
void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs
e)
{
if
(dataGridView1.Columns[e.ColumnIndex].Name == "CLICK")
{
string
id = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
string
name = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
string
location = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
string
date = dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString();
string
message = "Id : " + id + "\nName : " + name + "\nLocation : " + location + "\nDate : " + date;
MessageBox.Show(message);
}
}
}
}
|
After adding all columns generate the CellClick of the dataGridView.
Now add the below code into you cell click event. In click event I have
collected all the values of the row and displayed in the message box.
Here we have used static datatable and assign it to the dataGridView as datasource.
Now we have done run the application and check the output.
Now click on button to get the value.
0 comments:
Please let me know your view