DataGrid學習六

來源:互聯網
上載者:User
datagrid 前一樣本的一個問題是,當行可編輯時,主鍵欄位 (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();
}

相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。