Click here to Skip to main content
15,912,069 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Well actually im developing a windows based application... i was able place a button on my gridview table but the problem now is when i click on my table that contain the button wherever position i placed my button when i check the table value of rowindex =0 and columnindex=0 it returns the value of the button...

Assuming i created a table and i placed the button on column 4 but when i try to check whats the value of column 0 it returns the value of column 4 which is the value of the button... im really confused why is it so.... i can see the button is in the proper column but why is it that when i try to retrieve the value of column zero it return the value of the button located on column 4

please help me solve this problem guys and why is it, it acts that way?


heres the code that display my table and place a button in it

   public static void displayTable(DataGridView table, string query, List<int> b) { 
            if(con==null)
            {
                conSet();           
            }
            command = con.CreateCommand();
            command.CommandText = query;
            try {
                con.Open();
                
                dataset = new DataSet();
                dataset.CaseSensitive = true;

                adapter = new MySqlDataAdapter();
                adapter.SelectCommand = command;
                adapter.TableMappings.Add("Table","tbl_ipstatus");

                adapter.Fill(dataset);


                table.RowHeadersVisible = false;                
                //table.AutoGenerateColumns = true;                      

                
                table.DataSource = dataset.Tables["tbl_ipstatus"].DefaultView;
                
                table.MultiSelect = false;
                table.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                table.AllowUserToResizeRows = false;
                table.AllowUserToResizeColumns = false;
                table.AllowUserToAddRows = false;
                table.ReadOnly = true;                
             

                if (b.Count > 0)
                {
                    DataGridViewButtonColumn button = new DataGridViewButtonColumn();
                    button.Text = "hahaa";
                    button.HeaderText = "A Button";
                    button.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                    button.Width = 30;
                    button.UseColumnTextForButtonValue = true;
                    table.Columns.Insert(b.ElementAt(0), button);

                }     
                con.Close();
            }catch(Exception e){
                Console.WriteLine(e.Message);
                con.Close();
            
            }
        
        }
</int>



Here is the code that ask for the displaying of table with button

 public Miscellaneous()
       {
           InitializeComponent();
           this.CenterToScreen();
           buttonPosition.add(2);   //the position in the column where the button should appear in the table
           loadTable();    // load the table

       }

private void loadTable()
       {
       if (table.Columns.Count >= 3) // This line of code will delete the previous inserted button in the tablecolumn
               table.Columns.RemoveAt(3);
           DBConnect.displayTable(myTableToDisplay, "select * from tbl_itemprice", buttonPosition);
       }



       private void tableCode_CellClick(object sender, DataGridViewCellEventArgs e)
       {
           MessageBox.Show(myTableToDisplay[0, e.rowIndex].Value.toString());
       }


Assuming that the table that contains the column 0 has a text like "hello"
but when i click that it returns the word "hahaa" which is the value of the button located on column 2
Posted
Updated 14-Apr-11 0:13am
v3
Comments
Prerak Patel 14-Apr-11 5:06am    
Don't you like to share some code?
a1mimo 14-Apr-11 5:09am    
It would help if you share some code
[no name] 14-Apr-11 6:08am    
need help .....? share code.

Your MessageBox is showing the first column of the DataTable, not the DataGridView. Your source might be in a different order to the display
 
Share this answer
 
In following code u may try


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace TestWin
{
public partial class Form1 : Form
{
SqlConnection con = new SqlConnection();
List<int> buttonPosition = new List<int>();

public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
buttonPosition.Add(2); //the position in the column where the button should appear in the table
loadTable(); // load the table
}
public static void displayTable(DataGridView table, string query, List<int> b)
{
try {

DataSet dataset = new DataSet();
DataTable empTable = new DataTable();
DataRow dr;

empTable.Columns.Add(new DataColumn("Id"));
empTable.Columns.Add(new DataColumn("Name"));
empTable.Columns.Add(new DataColumn("Mobile"));
dr = empTable.NewRow();
dr["Id"] = "A";
dr["Name"] = "A....";
dr["Mobile"] = "12345";
empTable.Rows.Add(dr);
dr = empTable.NewRow();
dr["Id"] = "B";
dr["Name"] = "B....";
dr["Mobile"] = "12345656";
empTable.Rows.Add(dr);
dr = empTable.NewRow();
dr["Id"] = "C";
dr["Name"] = "C....";
dr["Mobile"] = "123343445";
empTable.Rows.Add(dr);
dataset.Tables.Add(empTable);
table.RowHeadersVisible = false;
//table.AutoGenerateColumns = true;
table.DataSource = dataset.Tables[0].DefaultView;
table.MultiSelect = false;
table.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
table.AllowUserToResizeRows = false;
table.AllowUserToResizeColumns = false;
table.AllowUserToAddRows = false;
table.ReadOnly = true;
if (b.Count > 0)
{
DataGridViewButtonColumn button = new DataGridViewButtonColumn();
button.Text = "hahaa";
button.HeaderText = "A Button";
button.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
button.Width = 30;
button.UseColumnTextForButtonValue = true;
table.Columns.Insert(b.ElementAt(0), button);
}

}catch(Exception e)
{
Console.WriteLine(e.Message);

}
}


private void loadTable()
{
displayTable(dataGridView1, "select * from tbl_itemprice", buttonPosition);
}
private void tableCode_CellClick(object sender, DataGridViewCellEventArgs e)
{
MessageBox.Show(dataGridView1[2, e.RowIndex].Value.ToString());
}

}
}

In line
MIDL
MessageBox.Show(dataGridView1[2, e.RowIndex].Value.ToString());


in place of 2 u may pass index of any colnm of which value u want to dispay (0,1,2 or 3)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900