GridView学习,常用记下来
先自定义一个表
前台
<div> <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" OnSelectedIndexChanged="GridView2_SelectedIndexChanged" Width="600px"> <Columns> <asp:TemplateField > <ItemTemplate> <input id="Radio1" type="radio" name="rad" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="prizename" HeaderText="入住酒店" /> <asp:BoundField DataField="point" HeaderText="多" /> <asp:BoundField DataField="number" HeaderText="式" /> <asp:BoundField DataField="totalpoint" FooterText="寺" HeaderText="朋" /> <asp:BoundField DataField="prizeid" FooterText="双" HeaderText="妇" /> <asp:CommandField SelectText="确认价格" HeaderText="操作" ShowSelectButton="True"> <ItemStyle Width="60px" HorizontalAlign="Center" /> </asp:CommandField> </Columns> </asp:GridView> </div>
后代码
protected void Button1_Click(object sender, EventArgs e) { DataTable dt = new DataTable("cart"); DataColumn dc1 = new DataColumn("prizename", Type.GetType("System.String")); DataColumn dc2 = new DataColumn("point", Type.GetType("System.Int16")); DataColumn dc3 = new DataColumn("number", Type.GetType("System.Int16")); DataColumn dc4 = new DataColumn("totalpoint", Type.GetType("System.Int64")); DataColumn dc5 = new DataColumn("prizeid", Type.GetType("System.String")); dt.Columns.Add(dc1); dt.Columns.Add(dc2); dt.Columns.Add(dc3); dt.Columns.Add(dc4); dt.Columns.Add(dc5); for (int i = 0; i < 10; i++) { DataRow dr = dt.NewRow(); dr["prizename"] = "娃娃"; dr["point"] = 10; dr["number"] = 1; dr["totalpoint"] = 10; dr["prizeid"] = "001"; dt.Rows.Add(dr); } GridView2.DataSource = dt; GridView2.DataBind(); } protected void GridView2_SelectedIndexChanged(object sender, EventArgs e) { Page.ClientScript.RegisterStartupScript(GetType(), "", "alert('aaa')", true); }