在C#中添加Datagridviewbuttoncolumn [英] Add Datagridviewbuttoncolumn in c#

查看:774
本文介绍了在C#中添加Datagridviewbuttoncolumn的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好...我想问问是否有人可以给我一些简单的代码,以介绍如何创建在该表的1或2列上具有自定义按钮的datagridview ...根据数据库项检索数据...这是数据库的结构...假设我的数据库名为"CAFEMS",表名称为"Computers"

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


所以我的计算机"表中包含了该计算机的IP地址记录以及一个指定的名称及其状态...我想要的是在datagridview中显示所有这些记录,但要稍加曲折...而不是仅显示3我想在datagridview的各列中再添加2列,所有这些额外的列都将包含一个datagridviewbuttoncolumn我已经在网上搜索了一些示例,但似乎找不到一个...我有一些有关如何从中检索数据的教程数据库数据集和mysqldataadapter,但这些教程似乎都没有指示如何在某些列上插入按钮,这正是我想要的


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|  |


如您所见,即时消息在第三和第五列上添加了一个按钮,我应该怎么做?到目前为止,我已经有了这段代码,但是这段代码只能从数据库中检索数据,但是我不知道如何在检索到的数据上插入datagridviewbuttoncolumn ....希望您能对我有所帮助...这是我的代码


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...

推荐答案

如果您正在开发Web应用程序并且只想在GridView中使用Button
您可以尝试

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

<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>


这篇关于在C#中添加Datagridviewbuttoncolumn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆