[ÕªÒª]ǰһʾÀýµÄÒ»¸öÎÊÌâÊÇ£¬µ±Ðпɱà¼Ê±£¬Ö÷¼ü×ֶΠ(au_id) Ò²³ÊÏÖΪÎı¾ÊäÈë¿ò¡£Äú²»Ï£Íû¿Í»§¶Ë¸ü¸Ä¸ÃÖµ£¬ÒòΪÐèÒªËüÀ´È·¶¨¸üÐÂÊý¾Ý¿âÖеÄÄÄÒ»ÐС£ÐÒÔ˵ÄÊÇ£¬¿ÉÒÔͨ¹ý¾«È·Ö¸¶¨Ã¿Ò»ÁÐÏà¶ÔÓڿɱà¼ÐеÄÍâ¹Û£¬½ûÓý«´ËÁгÊÏÖΪÎı¾¿ò¡£Îª´ËÐèÔÚ DataGrid µÄ Columns ¼¯ºÏÖж¨ÒåÿһÐУ¬Ê¹Óà Bo...
ǰһʾÀýµÄÒ»¸öÎÊÌâÊÇ£¬µ±Ðпɱà¼Ê±£¬Ö÷¼ü×ֶΠ(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();
}
……