前一示例的一个问题是,当行可编辑时,主键字段 (au_id) 也呈现为文本输入框。您不希望客户端更改该值,因为需要它来确定更新数据库中的哪一行。幸运的是,可以通过精确指定每一列相对于可编辑行的外观,禁用将此列呈现为文本框。为此需在 datagrid 的 columns 集合中定义每一行,使用 boundcolumn 控件分配每一列的数据字段。使用此方法,您可以完全控制列的顺序,以及它们的 readonly 属性。对于 au_id 列,将 readonly 属性设置为 true。当行处于编辑模式时,此列将继续呈现为标签。下面的示例说明此方法。
aspx文件里面加入datagrid控件半设置属性,代码如下:
<body ms_positioning="gridlayout">
<form id="form7" method="post" runat="server">
<asp:literal id="message" runat="server"></asp:literal>
<asp:datagrid id="datagrid1" runat="server" width="800" backcolor="#ccccff" bordercolor="black" showfooter="false" cellpadding="3" cellspacing="0" font-name="verdana" font-size="8pt" headerstyle-backcolor="#aaaadd" oneditcommand="mydatagrid_edit" oncancelcommand="mydatagrid_cancel" onupdatecommand="mydatagrid_update" datakeyfield="au_id" autogeneratecolumns="false">
<columns>
<asp:editcommandcolumn edittext="编辑" canceltext="取消" updatetext="更新" itemstyle-wrap="false" />
<asp:boundcolumn headertext="au_id" sortexpression="au_id" readonly="true" datafield="au_id" itemstyle-wrap="false" />
<asp:boundcolumn headertext="au_lname" sortexpression="au_lname" datafield="au_lname" />
<asp:boundcolumn headertext="au_fname" sortexpression="au_fname" datafield="au_fname" />
<asp:boundcolumn headertext="phone" sortexpression="phone" datafield="phone" />
<asp:boundcolumn headertext="address" sortexpression="address" datafield="address" />
<asp:boundcolumn headertext="city" sortexpression="city" datafield="city" />
<asp:boundcolumn headertext="state" sortexpression="state" datafield="state" />
<asp:boundcolumn headertext="zip" sortexpression="zip" datafield="zip" />
<asp:boundcolumn headertext="contract" sortexpression="contract" datafield="contract" />
</columns>
</asp:datagrid>
</form>
</body>
aspx.cs文件中加入下面代码:
导入using system.data.sqlclient;
定义sqlconnection myconnection;
private void page_load(object sender, system.eventargs e)
{
myconnection = new sqlconnection("user id=sa;password=;initial catalog=pubs;data source=jeff");
if (!ispostback)
bindgrid();
}
public void mydatagrid_edit(object sender, datagridcommandeventargs e)
{
datagrid1.edititemindex = (int)e.item.itemindex;
bindgrid();
}
public void mydatagrid_cancel(object sender, datagridcommandeventargs e)
{
datagrid1.edititemindex = -1;
bindgrid();
}
public void mydatagrid_update(object sender, datagridcommandeventargs e)
{
string updatecmd = "update authors set au_id = @id, au_lname = @lname, au_fname = @fname, phone = @phone, "
+ "address = @address, city = @city, state = @state, zip = @zip, contract = @contract where au_id = @id";
sqlcommand mycommand = new sqlcommand(updatecmd, myconnection);
mycommand.parameters.add(new sqlparameter("@id", sqldbtype.nvarchar, 11));
mycommand.parameters.add(new sqlparameter("@lname", sqldbtype.nvarchar, 40));
mycommand.parameters.add(new sqlparameter("@fname", sqldbtype.nvarchar, 20));
mycommand.parameters.add(new sqlparameter("@phone", sqldbtype.nchar, 12));
mycommand.parameters.add(new sqlparameter("@address", sqldbtype.nvarchar, 40));
mycommand.parameters.add(new sqlparameter("@city", sqldbtype.nvarchar, 20));
mycommand.parameters.add(new sqlparameter("@state", sqldbtype.nchar, 2));
mycommand.parameters.add(new sqlparameter("@zip", sqldbtype.nchar, 5));
mycommand.parameters.add(new sqlparameter("@contract", sqldbtype.nvarchar,1));
mycommand.parameters["@id"].value = datagrid1.datakeys[(int)e.item.itemindex];
string[] cols = {"@id","@lname","@fname","@phone","@address","@city","@state","@zip","@contract"};
int numcols = e.item.cells.count;
for (int i=2; i<numcols-1; i++) //跳过第一、第二和最后一列
{
string colvalue =((textbox)e.item.cells[i].controls[0]).text;
// 检查在所需字段中是否有空值
if (i<6 && colvalue == "")
{
message.text = "错误:“作者 id”、“姓名”或“电话”不允许使用空值";
return;
}
mycommand.parameters[cols[i-1]].value = colvalue;
}
//追加最后一行,将 true/false 值转换为 0/1
if (string.compare(((textbox)e.item.cells[numcols-1].controls[0]).text, "true", true)==0)
mycommand.parameters["@contract"].value = "1";
else
mycommand.parameters["@contract"].value = "0";
mycommand.connection.open();
try
{
mycommand.executenonquery();
message.text = "<b>已更新记录</b><br>" + updatecmd;
datagrid1.edititemindex = -1;
}
catch (sqlexception e)
{
if (e.number == 2627)
message.text = "错误:已存在具有相同主键的记录";
else
message.text = "错误:未能更新记录,请确保正确填写了字段";
}
mycommand.connection.close();
bindgrid();
}
public void bindgrid()
{
sqldataadapter mycommand = new sqldataadapter("select * from authors", myconnection);
dataset ds = new dataset();
mycommand.fill(ds, "authors");
datagrid1.datasource=ds.tables["authors"].defaultview;
datagrid1.databind();
}