<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None"
OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit">
<Columns>
<asp:BoundField HeaderText="id" DataField="id"
readonly="true" />
<asp:BoundField HeaderText=" 标题" DataField="title" />
<asp:BoundField HeaderText="作者" DataField="author" />
<asp:CommandField HeaderText="选择" ShowSelectButton="True" />
<asp:CommandField HeaderText="编辑" ShowEditButton="True" />
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
</Columns>
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
</asp:GridView>
OnRowDeleting="GridView1_RowDeleting" 删除是触发的事件,
OnRowEditing="GridView1_RowEditing" 编辑是触发的事件
OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit"> 更新时触发的事件
OnRowCancelingEdit="GridView1_RowCancelingEdit" 取消是触发的事件
string sqlstr = "select * from News_info";
DataTable dt = DataAccess.GetDataTable(sqlstr); 执行
sql语句的函数调用一个函数
GridView1.DataKeyNames = new string[] { "id" };//主键 绑定主键在事件中可以返回一个绑定值
GridView1.DataSource = dt;
GridView1.DataBind();
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
Label1.Text = GridView1.DataKeys[e.RowIndex].Value.ToString(); 要是没有绑定主键的话,这一句会报错,这一句取不到值 这一句返回的是去删除哪一行的主键
获取当中的选中行的字段值 ,具体要选中哪一行 在cells[写相应的数字]((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
}