Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dataGridView1_CellClick Determine value in column

Status
Not open for further replies.

kgreer

MIS
Jul 18, 2003
155
0
0
US
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.Name.Equals(tabToRemove, StringComparison.OrdinalIgnoreCase))
//{
tabMain.TabPages.RemoveAt(0);

//}
}

try
{
//SqlDataReader myReader = null;

try
{
myConnection.Open();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}

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;
dataAdapter.Fill(table);

// 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();
dataGridView1.Columns.Add(btn);
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;

newPage.Controls.Add(dataGridView1);

// Add to the tab control.
tabMain.TabPages.Add(newPage);
}
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e.ToString());
}
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
//WANTING TO DETERMINE WHAT VALUE IS IN COLUMN1///
}
 
No ALL CAPS next time. We can still hear your post.

Code:
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //WANTING TO DETERMINE WHAT VALUE IS IN COLUMN1///
            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();
        }

Lodlaiden

You've got questions and source code. We want both!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top