PC6下载站

分类分类

DataGrid学习六

关注+2004-10-07作者:蓝点

 前一示例的一个问题是,当行可编辑时,主键字段 (au_id) 也呈现为文本输入框。您不希望客户端更改该值,因为需要它来确定更新数据库中的哪一行。幸运的是,可以通过精确指定每一列相对于可编辑行的外观,禁用将此列呈现为文本框。为此需在 DataGrid 的 Columns 集合中定义每一行,使用 BoundColumn 控件分配每一列的数据字段。使用此方法,您可以完全控制列的顺序,以及它们的 ReadOnly 属性。对于 au_id 列,将 ReadOnly 属性设置为 true。当行处于编辑模式时,此列将继续呈现为标签。下面的示例说明此方法。 



aspx文件里面加入DataGrid控件半设置属性,代码如下:

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 {
  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 = "已更新记录
" + 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();
}
 

展开全部

相关文章

更多+相同厂商

热门推荐

  • 最新排行
  • 最热排行
  • 评分最高
排行榜

    点击查看更多

      点击查看更多

        点击查看更多

        说两句网友评论

          我要评论...
          取消