Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

dataGridView1_CellClick Determine value in column

dataGridView1_CellClick Determine value in column

dataGridView1_CellClick Determine value in column

Alright here is my issue.
In my application I am currently building TabControl based on dates from my database.  I then dynamically build DataGridView from another query inside the TabControl.  I am wanting to place a Edit button on each record on the DataGridView.  When I click the  button I am able to determine the RowIndex and ColumnIndex in the DataGridViewCellEventHandler.  My question is, how do I determine the value in Column1?  The button is in Column0.  Since I am building the DataGridView dynamically, I am not sure what the name is for each DataGridView. Below is the code for building the TabControl/DataGridView and the ClickEvenHandler.

 private void LoadTabs()

            int tabCount = tabMain.TabPages.Count;
            for (int i = 0; i < tabCount; i++)
                //if (tabMain.TabPages[i].Name.Equals(tabToRemove, StringComparison.OrdinalIgnoreCase))

                //SqlDataReader myReader = null;

                catch (Exception e)

                SqlDataAdapter da = new SqlDataAdapter("Select Distinct EDATE From TABLE Where PROCESS is NULL Order By EDATE", myConnection);
                DataSet dsPubs = new DataSet("Pubs");
                da.FillSchema(dsPubs, SchemaType.Source, "EmployeeDeduction");
                da.Fill(dsPubs, "EmployeeDeduction");

                DataTable tblEmployeeDeduction;
                tblEmployeeDeduction = dsPubs.Tables["EmployeeDeduction"];

                foreach (DataRow drCurrent in tblEmployeeDeduction.Rows)
                    string strSQL = "Select 'ID'=a.EMPNO,'Last Name'=LTRIM(RTRIM(b.Last_Name)),'First Name'=LTRIM(RTRIM(b.First_Name))," +
                            "'Ded Date'=LEFT(LTRIM(RTRIM(a.EDATE)),12),'DESC'=a.DEDDESCR,'Amount'=Sum(a.DEDAMOUNT),'GL #'=a.GLNUMBER " +
                            "From dbo.TABLE1" +
                            "as a join TABLE2 as b on a.EMPNO = b.Empl_No " +
                            "Where Process is null AND EDATE = '" + drCurrent[0].ToString() + "'" +
                            " Group By a.EmpNo,b.Last_Name,b.First_Name,a.EDate,a.DEDDESCR,a.GLNumber Order By 1 ";
                    SqlDataAdapter dataAdapter = new SqlDataAdapter(strSQL, myConnection);
                    SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
                    DataTable table = new DataTable();
                    table.Locale = System.Globalization.CultureInfo.InvariantCulture;

                    // The tabpage.
                    TabPage newPage = new TabPage(drCurrent[0].ToString());

                    newPage.Text = drCurrent[0].ToString();

                    DataGridView dataGridView1 = new DataGridView();
                    dataGridView1.AllowUserToAddRows = false;
                    dataGridView1.AllowUserToDeleteRows = false;
                    dataGridView1.AutoSize = true;
                    dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

                    DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
                    btn.HeaderText = "Edit";
                    btn.Text = "Edit";
                    btn.Name = "btn";
                    btn.UseColumnTextForButtonValue = true;

                    dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick);
                    dataGridView1.Dock = DockStyle.Fill;

                    dataGridView1.DataSource = table.DefaultView;


                    // Add to the tab control.
            catch (Exception e)
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)

RE: dataGridView1_CellClick Determine value in column

No ALL CAPS next time. We can still hear your post.


private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
            DataGridView dgv = new DataGridView();
            String s;
            //Cell that was clicked
            s = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();

            //The cell to the right
            s = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value.ToString();


You've got questions and source code. We want both!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close