如何在.net中点击事件时在gridview中显示数据?

时间:2016-01-05 06:26:13

标签: c# asp.net gridview

这里是cs:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="Book_id" EnablePersistedSelection="True" BackColor="White"  Height="240px" 
        Width="755px" BorderColor="Red" BorderWidth="2px">
        <Columns>
            <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" 
                ReadOnly="True" SortExpression="Book_id" >
            <ControlStyle BorderColor="Red" />
            </asp:BoundField>
            <asp:BoundField DataField="bookid" HeaderText="bookid" SortExpression="bookid" />
            <asp:BoundField DataField="studentid" HeaderText="studentid" SortExpression="studentid" />
            <asp:BoundField DataField="date" HeaderText="date" SortExpression="date" />
            <asp:BoundField DataField="returndate" HeaderText="returndate" SortExpression="returndate" />
             <asp:BoundField DataField="returnstatus" HeaderText="returnstatus" SortExpression="returnstatus" />

            </Columns>
            </asp:GridView>

ASPX:

{{1}}

以下是我执行的查询的screenshot

我是.net的新手,任何人都可以通过调用storedprocedure来指导我在gridview中显示数据。

谢谢,

1 个答案:

答案 0 :(得分:3)

试试这个

SqlConnection con = Connection.DBconnection();
SqlCommand com = new SqlCommand("sp_logdetails", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@bookid",1);
SqlDataAdapter adp = new SqlDataAdapter(com);
DataSet ds = new DataSet();
adp.Fill(ds);
GridView1.DataSource=ds.Tables[0];
GridView1.DataBind();

您需要将@bookid参数传递给SP。我已将@bookid的值硬编码为1,但您需要将其作为parameter传递。