Click here to Skip to main content
15,881,687 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello guys... I would like to ask if anyone can give me some simple code regarding on how to create a datagridview that has a customized button on 1 or 2 column of that table... by the way i want to retrieve the data based on my database items...this is the structure of my database... assuming my database is named as "CAFEMS" and the table name is "Computers"
id            |    name       |   status
--------------+---------------+------------
192.168.0.2   |   PC-2        |     Active
192.168.0.3   |   PC-2        |     Active
192.168.0.25  |   PC-2        |     Active
192.168.0.112 |   PC-2        |     Active
192.168.0.220 |   PC-2        |     Active
192.168.0.20  |   PC-2        |     Active

so my table "Computers" computers contain this records of ip addresses of computers and a specified name and its status... What i want is to display all this record in a datagridview but with a little twists... instead of just displaying 3 columns in the datagridview i want to add 2 more columns and all this additional column would contain a datagridviewbuttoncolumn i've search the net for some sample but cant seem to find one... i have some tutorial on how to retrieve data from the database dataset and mysqldataadapter but none of those tutorial seems to instruct on how to insert a button on certain column this is what i want
id            |    name   |  Activate  |   status   |     Inactive
--------------+-----------+------------+------------+-------------------|
192.168.0.2   |   PC-2    | |aButton|  |     Active |  |anotherButton|  |
192.168.0.3   |   PC-3    | |aButton|  |     Active |  |anotherButton|  |
192.168.0.25  |   PC-5    | |aButton|  |     Active |  |anotherButton|  |
192.168.0.112 |   PC-7    | |aButton|  |     Active |  |anotherButton|  |
192.168.0.220 |   PC-9    | |aButton|  |     Active |  |anotherButton|  |
192.168.0.20  |   PC-21   | |aButton|  |     Active |  |anotherButton|  |

As you can see im adding a button on the 3rd and 5th column how am i supposed to do it? i have this code so far but this code only retrieves the data from a database but i dont know how to insert the datagridviewbuttoncolumn on the retrieved data.... hope you can help me guys... this is my code
/* I have created all the variables as static like this */
public static MySQLCommand command;
public static MySQLConnection con;
public static MySQLDataAdapter adapter;

public static void displayTable(DataGridView table, string query) {
            if(con==null)
            {
                conSet();       //Assuming i have successfully connected to the DB
            }
            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.AllowUserToAddRows = true;
                DataGridViewButtonColumn button = new DataGridViewButtonColumn();
                button.Text = "aButton";
                table.DataSource = dataset.Tables["tbl_ipstatus"].DefaultView;
                con.Close();
            }catch(Exception e){
                Console.WriteLine(e.Message);

            }
        }

Hope you can help me with my problem guys thanks in advance...
Posted
Comments
Wild-Programmer 10-Apr-11 22:44pm    
Is it a Windows application or a Web application?

1 solution

If you are developing a Web Application and just want to Button in you GridView
You can Try

XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowPaging="True"
                        OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="2" OnRowCancelingEdit="GridView1_RowCancelingEdit"
                        OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
                        <Columns>
                            <asp:CommandField HeaderText="Edit" ShowEditButton="True" />
                            <asp:BoundField HeaderText="ProdId" DataField="ProductId" SortExpression="ProductId" />
                            <asp:BoundField HeaderText="ProdName" DataField="ProductName" SortExpression="ProductName" />
                            <asp:BoundField HeaderText="ProdPrice" DataField="ProductPrice" SortExpression="ProductPrice" />
                           <asp:TemplateField>
                           <ItemTemplate>
                               <asp:Button ID="Button1" runat="server" CommandName="ADD" Text="Button" />
                           </ItemTemplate>
                           </asp:TemplateField>
                           <asp:TemplateField>
                           <EditItemTemplate>
                           <asp:TextBox ID="txtId" runat="server" Width="80%" Height="50%"></asp:TextBox>
                           </EditItemTemplate>
                           </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
 
Share this answer
 
Comments
Madzmar25 14-Apr-11 5:00am    
Well actually im developing a windows based application... i was able to do it 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 in 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

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