Friday, 26 September 2014

How to Add Button Control and Retrieve Row Value on Button Click in DataGridView Using C#.Net, Windows Application

9/26/2014 - By Pranav Singh 0

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.


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.

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.


About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top