怎样用datagrid直接编辑一个数据库表的内容,这个表只有小于20行的数据,希望能通过datagrid直接显示编辑,就像编辑一个表格一样。 使用 DataGrid的 OnEditCommand="" OnCancelCommand="" OnUpdateCommand=""-------努力学习 不断实践 虚心讨教-------- 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <asp:DataGrid DataKeyField="LotteryID" id="classpicsDG" runat="server" Width="100%" AutoGenerateColumns="False" CellPadding="4" BackColor="White" BorderWidth="1px" BorderStyle="None" BorderColor="#3366CC"> <SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99" BackColor="#009999"></SelectedItemStyle> <ItemStyle ForeColor="#003399" BackColor="White"></ItemStyle> <HeaderStyle Font-Bold="True" Wrap="False" HorizontalAlign="Center" ForeColor="#CCCCFF" VerticalAlign="Middle" BackColor="#003399"></HeaderStyle> <FooterStyle ForeColor="#003399" BackColor="#99CCCC"></FooterStyle> <Columns> <asp:BoundColumn DataField="LotteryID" SortExpression="LotteryID" ReadOnly="True" HeaderText="序号"> <HeaderStyle Width="40px"></HeaderStyle> </asp:BoundColumn> <asp:BoundColumn DataField="HomeTeam" SortExpression="HomeTeam" ReadOnly="True" HeaderText="主队"> <HeaderStyle Width="200px"></HeaderStyle> </asp:BoundColumn> <asp:BoundColumn DataField="GuestTeam" SortExpression="GuestTeam" ReadOnly="True" HeaderText="客队"> <HeaderStyle Width="200px"></HeaderStyle> </asp:BoundColumn> <asp:BoundColumn DataField="FirstSelect" SortExpression="FirstSelect" HeaderText="首选"> <HeaderStyle Width="40px"></HeaderStyle> </asp:BoundColumn> <asp:BoundColumn DataField="FirstConfidence" SortExpression="FirstConfidence" HeaderText="信心"> <HeaderStyle Width="40px"></HeaderStyle> </asp:BoundColumn> <asp:BoundColumn DataField="SecondSelect" SortExpression="SecondSelect" HeaderText="次选"> <HeaderStyle Width="40px"></HeaderStyle> </asp:BoundColumn> <asp:BoundColumn DataField="SecondConfidence" SortExpression="SecondConfidence" HeaderText="信心"> <HeaderStyle Width="40px"></HeaderStyle> </asp:BoundColumn> <asp:BoundColumn DataField="LastSelect" SortExpression="LastSelect" HeaderText="末选"> <HeaderStyle Width="40px"></HeaderStyle> </asp:BoundColumn> <asp:BoundColumn DataField="LastConfidence" SortExpression="LastConfidence" HeaderText="信心"> <HeaderStyle Width="40px"></HeaderStyle> </asp:BoundColumn> <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" CancelText="取消" EditText="编辑"></asp:EditCommandColumn> </Columns> <PagerStyle Visible="False" HorizontalAlign="Left" ForeColor="#003399" BackColor="#99CCCC" Mode="NumericPages"></PagerStyle></asp:DataGrid>// .cs /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.lnkOK.Click += new System.EventHandler(this.lnkOK_Click); this.Load += new System.EventHandler(this.Page_Load); this.classpicsDG.CancelCommand +=new DataGridCommandEventHandler(classpicsDG_CancelCommand); this.classpicsDG.UpdateCommand +=new DataGridCommandEventHandler(classpicsDG_UpdateCommand); this.classpicsDG.EditCommand +=new DataGridCommandEventHandler(classpicsDG_EditCommand); this.classpicsDG.ItemCreated +=new DataGridItemEventHandler(classpicsDG_ItemCreated); } private void classpicsDG_CancelCommand(object source, DataGridCommandEventArgs e) { this.classpicsDG.EditItemIndex = -1; UpdateView(); } private void classpicsDG_EditCommand(object source, DataGridCommandEventArgs e) { this.classpicsDG.EditItemIndex = e.Item.ItemIndex; UpdateView(); } private void classpicsDG_UpdateCommand(object source, DataGridCommandEventArgs e) { const string UPDATE = " Update LotteryDetail Set FirstSelect = ?,FirstConfidence = ? , SecondSelect = ?, SecondConfidence = ? , LastSelect = ?, LastConfidence = ? Where LotteryID = ?"; DbRoot db = DbFactory.GetDb(); DbSqlText sql = new DbSqlText(UPDATE); for(int i = 3 ; i < 9 ; i++) { TextBox text = (TextBox)e.Item.Cells[i].Controls[0]; sql.SetValue(text.Text.Replace(" ","")); } string key = classpicsDG.DataKeys[e.Item.ItemIndex].ToString(); sql.SetValue(7,key); db.Exec(sql.SqlCmd ); UpdateView(); } private void UpdateView() { DataSet ds = DbFactory.GetDb().FillDataSet("SELECT * FROM LotteryDetail "); if(ds!= null && ds.Tables.Count > 0) { classpicsDG.DataSource = ds.Tables[0]; } classpicsDG.DataBind(); } private void classpicsDG_ItemCreated(object sender, DataGridItemEventArgs e) { ListItemType li=e.Item.ItemType; if(li==ListItemType.EditItem) { for(int i= 3; i <9 ; i++) { TextBox txtNum=(TextBox)e.Item.Cells[i].Controls[0];//第三列的文本框 txtNum.Width=Unit.Pixel(40); //在这里设置宽度 } } }/// .cs 中的东西有些是我自己的底层库,不要照搬,改成你自己的 union 分页 gridview的问题 散分(1')个人站点相互做友情链接 请推荐:控件开发和水晶报表的好书。 DataTable添加问题 就急,请大家帮忙,谢谢了.做个 带无限分类(按指定模板生成静态页) 的东西 在命令窗口里打aspnet_regiis -c提示,命令无效 asp.net+sql2000+oracle???? 请教如何获得请求当前页面的页面URL信息?? 急征成都地区网络开发高手!!! 关于datagrid的分页问题 人事考勤工资管理系统的设计方法!!
BackColor="White" BorderWidth="1px" BorderStyle="None" BorderColor="#3366CC">
<SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99" BackColor="#009999"></SelectedItemStyle>
<ItemStyle ForeColor="#003399" BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" Wrap="False" HorizontalAlign="Center" ForeColor="#CCCCFF" VerticalAlign="Middle"
BackColor="#003399"></HeaderStyle>
<FooterStyle ForeColor="#003399" BackColor="#99CCCC"></FooterStyle>
<Columns>
<asp:BoundColumn DataField="LotteryID" SortExpression="LotteryID" ReadOnly="True" HeaderText="序号">
<HeaderStyle Width="40px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="HomeTeam" SortExpression="HomeTeam" ReadOnly="True" HeaderText="主队">
<HeaderStyle Width="200px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="GuestTeam" SortExpression="GuestTeam" ReadOnly="True" HeaderText="客队">
<HeaderStyle Width="200px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="FirstSelect" SortExpression="FirstSelect" HeaderText="首选">
<HeaderStyle Width="40px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="FirstConfidence" SortExpression="FirstConfidence" HeaderText="信心">
<HeaderStyle Width="40px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="SecondSelect" SortExpression="SecondSelect" HeaderText="次选">
<HeaderStyle Width="40px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="SecondConfidence" SortExpression="SecondConfidence" HeaderText="信心">
<HeaderStyle Width="40px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="LastSelect" SortExpression="LastSelect" HeaderText="末选">
<HeaderStyle Width="40px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="LastConfidence" SortExpression="LastConfidence" HeaderText="信心">
<HeaderStyle Width="40px"></HeaderStyle>
</asp:BoundColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" CancelText="取消" EditText="编辑"></asp:EditCommandColumn>
</Columns>
<PagerStyle Visible="False" HorizontalAlign="Left" ForeColor="#003399" BackColor="#99CCCC" Mode="NumericPages"></PagerStyle>
</asp:DataGrid>
// .cs
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.lnkOK.Click += new System.EventHandler(this.lnkOK_Click);
this.Load += new System.EventHandler(this.Page_Load);
this.classpicsDG.CancelCommand +=new DataGridCommandEventHandler(classpicsDG_CancelCommand);
this.classpicsDG.UpdateCommand +=new DataGridCommandEventHandler(classpicsDG_UpdateCommand);
this.classpicsDG.EditCommand +=new DataGridCommandEventHandler(classpicsDG_EditCommand);
this.classpicsDG.ItemCreated +=new DataGridItemEventHandler(classpicsDG_ItemCreated);
}
private void classpicsDG_CancelCommand(object source, DataGridCommandEventArgs e)
{
this.classpicsDG.EditItemIndex = -1; UpdateView();
} private void classpicsDG_EditCommand(object source, DataGridCommandEventArgs e)
{
this.classpicsDG.EditItemIndex = e.Item.ItemIndex; UpdateView();
} private void classpicsDG_UpdateCommand(object source, DataGridCommandEventArgs e)
{
const string UPDATE = " Update LotteryDetail Set FirstSelect = ?,FirstConfidence = ? , SecondSelect = ?, SecondConfidence = ? , LastSelect = ?, LastConfidence = ? Where LotteryID = ?";
DbRoot db = DbFactory.GetDb();
DbSqlText sql = new DbSqlText(UPDATE); for(int i = 3 ; i < 9 ; i++)
{
TextBox text = (TextBox)e.Item.Cells[i].Controls[0];
sql.SetValue(text.Text.Replace(" ",""));
}
string key = classpicsDG.DataKeys[e.Item.ItemIndex].ToString(); sql.SetValue(7,key);
db.Exec(sql.SqlCmd ); UpdateView(); } private void UpdateView()
{
DataSet ds = DbFactory.GetDb().FillDataSet("SELECT * FROM LotteryDetail "); if(ds!= null && ds.Tables.Count > 0)
{
classpicsDG.DataSource = ds.Tables[0];
}
classpicsDG.DataBind();
} private void classpicsDG_ItemCreated(object sender, DataGridItemEventArgs e)
{
ListItemType li=e.Item.ItemType;
if(li==ListItemType.EditItem)
{
for(int i= 3; i <9 ; i++)
{
TextBox txtNum=(TextBox)e.Item.Cells[i].Controls[0];//第三列的文本框
txtNum.Width=Unit.Pixel(40); //在这里设置宽度
}
}
}/// .cs 中的东西有些是我自己的底层库,不要照搬,改成你自己的