如何在Grid中新增一条记录?

yyh30 2004-07-03 11:42:54
我的意思是按下新增按纽后,dataGrid会空出一行,让客户可以输入数据,跟delphi中的grid使用方式 一样,应该怎么做?
...全文
332 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
yyh30 2004-07-10
  • 打赏
  • 举报
回复
好了,謝謝各位了,我明白了,grid在new row后會空出一行,不過是在最后,並不是在當前的指針處
yyh30 2004-07-08
  • 打赏
  • 举报
回复
就是winform,謝謝各位了
  • 打赏
  • 举报
回复
关注ing~~
vzxq 2004-07-07
  • 打赏
  • 举报
回复
我也很关注这个问题》
jsp 中用js 能实现
可是datagird 想知道?
UP
lxcc 2004-07-07
  • 打赏
  • 举报
回复
winform?
yyh30 2004-07-07
  • 打赏
  • 举报
回复
好像各位讲的跟我的想法不符合,其实,我的问题很简单,就是说窗体上只有一个工具栏和一个grid,新增,删除这些按纽在工具栏上,那每次新增数据时,当我按下新增后,grid就要自动空出一行,让用户输入数据,然后存盘.我就是不知道用什么命令,grid会空出一行?
yyh30 2004-07-06
  • 打赏
  • 举报
回复
好的,谢谢各位,让我先来测试一下
qunw 2004-07-05
  • 打赏
  • 举报
回复
直接在DataGrid的每一行的Footer中添加一个TextBox设置Footer为不可视,电击按钮后再作设置Footer为可视,添加数据保存后,判断是否继续添加 yes 依然可视保持在添加]状态, no不可视.
lxcc 2004-07-05
  • 打赏
  • 举报
回复
<html>
<head>
</head>
<body style="FONT-FAMILY: arial">
<h2>Editable Data Grid
</h2>
<hr size="1" />
<form runat="server">
<asp:datagrid id="dgUserInfo" runat="server" DataKeyField="au_id" OnItemCommand="DataGrid_ItemCommand" OnEditCommand="DataGrid_Edit" OnUpdateCommand="DataGrid_Update" OnCancelCommand="DataGrid_Cancel" OnDeleteCommand="DataGrid_Delete" AllowPaging="true" PageSize="6" OnPageIndexChanged="DataGrid_Page" ForeColor="Black" BackColor="White" CellPadding="3" GridLines="None" CellSpacing="1" width="80%">
<FooterStyle backcolor="#C6C3C6"></FooterStyle>
<HeaderStyle font-bold="True" forecolor="White" backcolor="#4A3C8C"></HeaderStyle>
<PagerStyle font-size="Smaller" horizontalalign="Right" backcolor="#C6C3C6" mode="NumericPages"></PagerStyle>
<ItemStyle backcolor="#DEDFDE"></ItemStyle>
<Columns>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" CancelText="Cancel" EditText="Edit">
<ItemStyle font-size="Smaller" width="10%"></ItemStyle>
</asp:EditCommandColumn>
<asp:ButtonColumn Text="Delete" CommandName="Delete">
<ItemStyle font-size="Smaller" width="10%"></ItemStyle>
</asp:ButtonColumn>
</Columns>
</asp:datagrid>
<br />
<asp:LinkButton id="LinkButton1" onclick="AddNew_Click" runat="server" Text="Add new item" Font-Size="smaller"></asp:LinkButton>
<br />
<br />
<asp:Label id="Message" runat="server" width="80%" forecolor="red" enableviewstate="false"></asp:Label>
</form>
</body>
</html>
gkwww 2004-07-05
  • 打赏
  • 举报
回复
直接用 Adapter.update(DataTable)的话就不用写插入语句了。
lxcc 2004-07-05
  • 打赏
  • 举报
回复
// TODO: update the ConnectionString and Command values for your application

string ConnectionString = "server=(local);database=pubs;trusted_connection=true";
string SelectCommand = "SELECT au_id, au_lname, au_fname from Authors";

bool isEditing = false;

void Page_Load(object sender, EventArgs e) {

if (!Page.IsPostBack) {

// Databind the data grid on the first request only
// (on postback, bind only in editing, paging and sorting commands)

BindGrid();
}
}

// ---------------------------------------------------------------
//
// DataGrid Commands: Page, Sort, Edit, Update, Cancel, Delete
//

void DataGrid_ItemCommand(object sender, DataGridCommandEventArgs e) {

// this event fires prior to all of the other commands
// use it to provide a more graceful transition out of edit mode

CheckIsEditing(e.CommandName);
}

void CheckIsEditing(string commandName) {

if (dgUserInfo.EditItemIndex != -1) {

// we are currently editing a row
if (commandName != "Cancel" && commandName != "Update") {

// user's edit changes (if any) will not be committed
Message.Text = "Your changes have not been saved yet. Please press update to save your changes, or cancel to discard your changes, before selecting another item.";
isEditing = true;
}
}
}

void DataGrid_Edit(object sender, DataGridCommandEventArgs e) {

// turn on editing for the selected row

if (!isEditing) {
dgUserInfo.EditItemIndex = e.Item.ItemIndex;
BindGrid();
}
}

void DataGrid_Update(object sender, DataGridCommandEventArgs e) {

// update the database with the new values

// get the edit text boxes
string id = ((TextBox)e.Item.Cells[2].Controls[0]).Text;
string lname = ((TextBox)e.Item.Cells[3].Controls[0]).Text;
string fname = ((TextBox)e.Item.Cells[4].Controls[0]).Text;

// TODO: update the Command value for your application
SqlConnection myConnection = new SqlConnection(ConnectionString);
SqlCommand UpdateCommand = new SqlCommand();
UpdateCommand.Connection = myConnection;

if (AddingNew)
UpdateCommand.CommandText = "INSERT INTO authors(au_id, au_lname, au_fname, contract) VALUES (@au_id, @au_lname, @au_fname, 0)";
else
UpdateCommand.CommandText = "UPDATE authors SET au_lname = @au_lname, au_fname = @au_fname WHERE au_id = @au_id";

UpdateCommand.Parameters.Add("@au_id", SqlDbType.VarChar, 11).Value = id;
UpdateCommand.Parameters.Add("@au_lname", SqlDbType.VarChar, 40).Value = lname;
UpdateCommand.Parameters.Add("@au_fname", SqlDbType.VarChar, 20).Value = fname;

// execute the command
try {
myConnection.Open();
UpdateCommand.ExecuteNonQuery();
}
catch (Exception ex) {
Message.Text = ex.ToString();
}
finally {
myConnection.Close();
}

// Resort the grid for new records
if (AddingNew) {

dgUserInfo.CurrentPageIndex = 0;
AddingNew = false;
}

// rebind the grid
dgUserInfo.EditItemIndex = -1;
BindGrid();
}

void DataGrid_Cancel(object sender, DataGridCommandEventArgs e) {

// cancel editing

dgUserInfo.EditItemIndex = -1;
BindGrid();

AddingNew = false;
}

void DataGrid_Delete(object sender, DataGridCommandEventArgs e) {

// delete the selected row

if (!isEditing) {

// the key value for this row is in the DataKeys collection
string keyValue = (string)dgUserInfo.DataKeys[e.Item.ItemIndex];

// TODO: update the Command value for your application
SqlConnection myConnection = new SqlConnection(ConnectionString);
SqlCommand DeleteCommand = new SqlCommand("DELETE from authors where au_id='" + keyValue + "'", myConnection);

// execute the command
myConnection.Open();
DeleteCommand.ExecuteNonQuery();
myConnection.Close();

// rebind the grid
dgUserInfo.CurrentPageIndex = 0;
dgUserInfo.EditItemIndex = -1;
BindGrid();
}
}

void DataGrid_Page(object sender, DataGridPageChangedEventArgs e) {

// display a new page of data

if (!isEditing) {
dgUserInfo.EditItemIndex = -1;
dgUserInfo.CurrentPageIndex = e.NewPageIndex;
BindGrid();
}
}

void AddNew_Click(Object sender, EventArgs e) {

// add a new row to the end of the data, and set editing mode 'on'

CheckIsEditing("");

if (!isEditing) {

// set the flag so we know to do an insert at Update time
AddingNew = true;

// add new row to the end of the dataset after binding

// first get the data
SqlConnection myConnection = new SqlConnection(ConnectionString);
SqlDataAdapter myCommand = new SqlDataAdapter(SelectCommand, myConnection);

DataSet ds = new DataSet();
myCommand.Fill(ds);

// add a new blank row to the end of the data
object[] rowValues = { "", "", "" };
ds.Tables[0].Rows.Add(rowValues);

// figure out the EditItemIndex, last record on last page
int recordCount = ds.Tables[0].Rows.Count;
if (recordCount > 1)
recordCount--;
dgUserInfo.CurrentPageIndex = recordCount/dgUserInfo.PageSize;
dgUserInfo.EditItemIndex = recordCount%dgUserInfo.PageSize;

// databind
dgUserInfo.DataSource = ds;
dgUserInfo.DataBind();
}
}

// ---------------------------------------------------------------
//
// Helpers Methods:
//

// property to keep track of whether we are adding a new record,
// and save it in viewstate between postbacks

protected bool AddingNew {

get {
object o = ViewState["AddingNew"];
return (o == null) ? false : (bool)o;
}
set {
ViewState["AddingNew"] = value;
}
}

void BindGrid() {

SqlConnection myConnection = new SqlConnection(ConnectionString);
SqlDataAdapter myCommand = new SqlDataAdapter(SelectCommand, myConnection);

DataSet ds = new DataSet();
myCommand.Fill(ds);

dgUserInfo.DataSource = ds;
dgUserInfo.DataBind();
}
yyh30 2004-07-05
  • 打赏
  • 举报
回复
datagrid中添加一个空行,那这样的话,更新数据库还要不要写语句更新,谢谢
酋长 2004-07-04
  • 打赏
  • 举报
回复
datagrid中添加一个空行,然后设置该行的状态为编辑状态
bizbuy 2004-07-04
  • 打赏
  • 举报
回复
gz
源码下载地址: https://pan.quark.cn/s/8d2c461c797c JavaWeb程序设计构成了掌握Web交互式应用程序开发的核心领域,对于初学者来说,精通这一技术具有决定性意义。在“JavaWeb程序设计(第三版)作业答案”,我们可以预期获得针对该教材习题的一系列深入解析,从而协助学习者强化知识体系。 JavaWeb所包含的技术组件涵盖了Servlet、JSP(JavaServer Pages)、JDBC(Java Database Connectivity)以及各类框架如Spring MVC、Struts等。Servlet是Java平台提供的一种扩展服务器功能的接口,能够处理HTTP请求并生成相应的反馈。JSP则是一种用于构建动态网页的工具,它支持开发者将HTML代码与Java代码进行整合编写,从而简化了Web应用程序的开发流程。 作业答案通常会涉及以下几个核心内容: 1. **Servlet基础**:可能包含Servlet生命周期、init(), service(), destroy()方法的应用,以及如何在web.xml文件设定Servlet的映射关系。 2. **JSP基础**:JSP的九大内置对象,如request、response、session、application等的使用,以及EL(Expression Language)和JSTL(JavaServer Pages Standard Tag Library)的实际操作。 3. **HTTP协议理解**:GET和POST请求方法的差异,请求头与响应头的应用,以及会话管理的概念阐释。 4. **JDBC数据库操作**:与数据库建立连接,执行SQL指令,处理查询结果集,以及...
源码链接: https://pan.quark.cn/s/a4b39357ea24 斐讯K2是一款广受用户青睐的无线路由器,其运行表现稳定且具备较高的可操作性,在DIY爱好者群体拥有极高的声誉。本资料将系统性地阐述斐讯K2的固件刷机方法及其关联的技术要点。固件升级是路由器爱好者改善设备性能、扩展功能的一种普遍手段,经由替换出厂固件,能够达成更加个性化的网络配置、增强安全防护等目标。斐讯K2固件资源库涵盖了多种知名的非官方固件,诸如Tomato Pheonix 不死鸟、高恪、PandoraBox 潘多拉等,这些固件均具备独特的优势,能够适配不同用户的需求。 1. Tomato Pheonix 不死鸟:Tomato是一款立足于Linux的开源固件,以其精巧、高效而备受推崇。不死鸟版本是专门为华硕及斐讯路由器优化的分支,提供了卓越的QoS(服务质量)配置、详尽的图表监控以及便捷的固件升级途径。对于那些需要精准调控带宽和监测网络状态的用户而言,这是一个理想的选项。 2. 高恪:高恪固件是OpenWrt的定制化版本,着重于操作的便捷性和运行的可靠性,特别适合对路由器操作不甚熟悉的用户群体。它提供了一些实用的功能,例如内置的广告屏蔽、快速测速工具等,同时保留了OpenWrt的适应性。 3. PandoraBox 潘多拉:潘多拉盒是另一款基于OpenWrt的固件,它以丰富的插件库和强大的自定义潜力而闻名。用户能够依据个人需求安装各类插件,实现更多功能,如远程接入、DDNS(动态域名解析服务)等。 4. 官方固件的纯净版本与定制版本:官方固件通常更侧重于稳定性,纯净版意味着未预置额外的应用或服务,适合注重稳定性的用户。定制版则可能包含了制造商的特色功能或优...

111,130

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧