改善gridview的工作 [英] improve the working of gridview

查看:62
本文介绍了改善gridview的工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想为新数据库创建Gridview(数据库没有数据).

我想要在单击按钮时给我空的gridview,通过该gridview我可以将数据插入数据库中.

我的代码中的问题是,在使用gridview插入之前,应该在数据库中插入一些数据

这是我的ASP页面代码:

Hi
I want to create Gridview for the new database ( The database dose not have data).

I want when click on the button it give me empty gridview,by this gridview can I insert the data to the database.

The problem in my code is,it should be insert some data in the database before using the gridview to insert

This is my asp page code:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            DataSourceID="SqlDataSource1" ShowFooter="True">
            <Columns>
                <asp:BoundField DataField="RowNumber" HeaderText="RowNumber"
                    InsertVisible="False" ReadOnly="True" SortExpression="RowNumber" />
                   <asp:TemplateField HeaderText="Column1" SortExpression="Column1">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text=''<%# Bind("Column1") %>''></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtColumn1" runat="server" Height="22px" Width="145px"></asp:TextBox>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text=''<%# Bind("Column1") %>''></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>

                 <asp:TemplateField HeaderText="Column2" SortExpression="Column2">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox2" runat="server" Text=''<%# Bind("Column2") %>''></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtColumn2" runat="server" Height="22px" Width="203px"></asp:TextBox>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text=''<%# Bind("Column2") %>''></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>


                     <asp:TemplateField HeaderText="Column3" SortExpression="Column3" Visible="false">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox3" runat="server" Text=''<%# Bind("Column3") %>''></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtColumn3" runat="server" Height="22px" Width="203px"></asp:TextBox>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server" Text=''<%# Bind("Column3") %>''></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>

            </Columns>

        </asp:GridView>

         <asp:Button ID="Button1" runat="server" Text="Save" onclick="Button1_Click" />

        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:ourProjectConnectionString %>"
              InsertCommand="INSERT INTO [Table1] ([Column1], [Column2], [Column3]) VALUES (@Column1, @Column2, @Column3)"
            SelectCommand="SELECT * FROM [Table1]">

              <InsertParameters>
                <asp:Parameter Name="Column1" Type="String" />
                <asp:Parameter Name="Column2" Type="String" />
                <asp:Parameter Name="Column3" Type="String" />
            </InsertParameters>

            </asp:SqlDataSource>



C#代码的页面是:





and the page of c# code is :



protected void Button1_Click(object sender, EventArgs e)
        {
            TextBox txtColumn1 = GridView1.FooterRow.FindControl("txtColumn1") as TextBox;
            TextBox txtColumn2 = GridView1.FooterRow.FindControl("txtColumn2") as TextBox;
            TextBox txtColumn3 = GridView1.FooterRow.FindControl("txtColumn3") as TextBox;
            SqlDataSource1.InsertParameters["Column1"].DefaultValue = txtColumn1.Text;
            SqlDataSource1.InsertParameters["Column2"].DefaultValue = txtColumn2.Text;
            SqlDataSource1.InsertParameters["Column3"].DefaultValue = txtColumn3.Text;
            SqlDataSource1.Insert();
            GridView1.DataBind();
        }



有人可以帮助我吗?

在此先感谢.



Any one can help me ?

Thanks in Advance.

推荐答案

ConnectionStrings:ourProjectConnectionString%> InsertCommand ="INSERT INTO [Table1]([Column1],[Column2],[Column3])值(@ Column1,@ Column2,@ Column3)" SelectCommand ="SELECT * FROM [Table1]"> < InsertParameters> < asp:参数名称="Column1"类型="String"/> < asp:参数名称="Column2"类型="String"/> < asp:参数名称="Column3"类型="String"/> </InsertParameters> </asp:SqlDataSource>
ConnectionStrings:ourProjectConnectionString %>" InsertCommand="INSERT INTO [Table1] ([Column1], [Column2], [Column3]) VALUES (@Column1, @Column2, @Column3)" SelectCommand="SELECT * FROM [Table1]"> <InsertParameters> <asp:Parameter Name="Column1" Type="String" /> <asp:Parameter Name="Column2" Type="String" /> <asp:Parameter Name="Column3" Type="String" /> </InsertParameters> </asp:SqlDataSource>



C#代码的页面是:





and the page of c# code is :



protected void Button1_Click(object sender, EventArgs e)
        {
            TextBox txtColumn1 = GridView1.FooterRow.FindControl("txtColumn1") as TextBox;
            TextBox txtColumn2 = GridView1.FooterRow.FindControl("txtColumn2") as TextBox;
            TextBox txtColumn3 = GridView1.FooterRow.FindControl("txtColumn3") as TextBox;
            SqlDataSource1.InsertParameters["Column1"].DefaultValue = txtColumn1.Text;
            SqlDataSource1.InsertParameters["Column2"].DefaultValue = txtColumn2.Text;
            SqlDataSource1.InsertParameters["Column3"].DefaultValue = txtColumn3.Text;
            SqlDataSource1.Insert();
            GridView1.DataBind();
        }



有人可以帮助我吗?

预先感谢.



Any one can help me ?

Thanks in Advance.


看看这个Editable Gridview示例.

在ASP.NET 2.0中可编辑的GridView [
Have a look at this Editable Gridview example.

Editable GridView in ASP.NET 2.0[^]

By having this as a referance you can shape your gridview the way you want.


如果数据库中没有数据,并且您希望GridView显示允许客户端使用的控件要插入数据,您需要修改EmptyDataTemplate以为要插入的每个值包括一个按钮(如果需要,也可以是一个LinkBut​​ton)和一个文本框(或其他控件,如下拉列表,具体取决于情况)

If there is no data in the database and you''re wanting the GridView to display controls to allow the client to insert data you''ll need to modify the EmptyDataTemplate to include a button (could also be a LinkButton if you prefer) and a textbox (or another control like the dropdownlist depending on the situation) for each value you want to insert

<EmptyDataTemplate>
        <table>
            <tr>
                <td>
                    <asp:Button ID="Button1" runat="server" Text="Insert" CommandName="EmptyInsert" />
                </td>
                <td><asp:TextBox ID="TextBox1" runat="server" Text=''''></asp:TextBox></td>
                <td><asp:TextBox ID="TextBox2" runat="server" Text=''''></asp:TextBox></td>
                <td><asp:TextBox ID="TextBox3" runat="server" Text=''''></asp:TextBox></td>
            </tr>
        </table>
    </EmptyDataTemplate>





请注意,我在Button控件上设置了CommandName属性.您将需要使用gridview的OnRowCommand事件,并且在其后面的代码中应该看起来像





Notice that I have the CommandName Property set on the Button control. You''ll need to use the OnRowCommand event of the gridview and in the code behind it should look something like

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            // Check that the command name matches that from the button
            if (e.CommandName.CompareTo("EmptyInsert").Equals(0))
            {
                GridView gv = sender as GridView;
                Table tbl = gv.Controls[0] as Table;
                string input1 = (tbl.Rows[0].FindControl("TextBox1") as TextBox).Text;
                string input2 = (tbl.Rows[0].FindControl("TextBox2") as TextBox).Text;
                string input3 = (tbl.Rows[0].FindControl("TextBox3") as TextBox).Text;

                // Clear the initial values
                SqlDataSource1.InsertParameters.Clear();
                SqlDataSource1.InsertParameters.Add(new Parameter("column1", TypeCode.String, input1));
                SqlDataSource1.InsertParameters.Add(new Parameter("column2", TypeCode.Int32, input2));
                SqlDataSource1.InsertParameters.Add(new Parameter("column3", TypeCode.Boolean, input3));

                SqlDataSource1.Insert();
                gv.DataBind();
            }
        }


在这里,我们检查当前命令名称是否与插入按钮控件中的命令名称相同.接下来,我们在gridview的子表控件中找到文本框控件,并从每个控件中提取Text值.之后,我们清除现有的插入参数并输入新的插入参数(在此我以随机类型为例).最后,我们使用SqlDataSource插入数据并重新绑定gridview以显示更新的数据库值.


Here we check that the current command name is equal to the command name from the insert button control. Next we find the textbox controls in the gridview''s childtable control and pull the Text value from each one. After that we clear the existing insert parameters and enter our new ones ( I used random types here as an example). Finally, we use the SqlDataSource to insert the data and rebind our gridview to show the updated database values.


这篇关于改善gridview的工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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