2012-04-09 40 views
1

我正在使用gridview编辑来编辑我在gridview中的值,当我按下edit时,所有列都可以编辑,我希望其中一列不允许被编辑。如何在gridview中将列设置为只读

有什么办法可以做到这一点?

这是我的aspx代码:

<asp:GridView ID="GridView1" runat="server" AutoGenerateDeleteButton="True" 
      onrowdeleting="GridView1_RowDeleting" AutoGenerateEditButton="True" 
      onrowediting="GridView1_RowEditing" 
      onrowcancelingedit="GridView1_RowCancelingEdit" 
      onrowupdating="GridView1_RowUpdating" > 
</asp:GridView> 

这是我的aspx.cs代码:

public void loadCustomer() 
     { 
      SqlConnection objConnection = new SqlConnection("Data Source=localhost;Initial Catalog=SampleApplication;Integrated Security=True"); 
      objConnection.Open(); 
      SqlCommand objCommand = new SqlCommand(); 
      objCommand.CommandText = "Select * from Customer"; 
      objCommand.Connection = objConnection; 
      objCommand.ExecuteNonQuery(); 
      DataSet objds = new DataSet(); 
      SqlDataAdapter objadap = new SqlDataAdapter(objCommand); 
      objadap.Fill(objds); 
      GridView1.DataSource = objds.Tables[0]; 
      GridView1.DataBind(); 
      objConnection.Close(); 
     } 
+0

访问此链接:http://forums.asp.net/p/1442129/3268947.aspx – 2012-04-09 10:12:41

回答

-1

你需要给权利"ReadOnly= true"该列,你不喜欢被编辑。

例如。

GridView1.columns[1].ReadOnly= true; 

您可以在GridView的RowDataBound事件中使用此行。 。

1

gridView1的RowDataBound事件

((绑定列)gridView1.Columns [columnIndex])只读= TRUE;

+1

提供了一些细节。 – Parixit 2014-02-11 12:14:00

2

我知道这确实很老,但我需要为其他分享我的问题的人提供答案。无论如何,我现在一直在不停地挣扎几天。当你的问题明确地发布在C#中时,每个人似乎都会发布VB代码。

什么你要找的是:

protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     e.Row.Cells[columntobedisabled].Enabled = false; 
    } 

在哪里被禁用之列“columntobedisabled”是指数...如。 1