1

我正在使用 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();
        }
4

4 回答 4

3

gridView1 的 RowDataBound 事件

((BoundField)gridView1.Columns[columnIndex]).ReadOnly = true;

于 2014-02-11T11:57:30.117 回答
2

I know this is really old but I need to put the answer here for others who shared my issue. Regardless, I've been struggling with this non-stop for a couple of days now. Everyone seems to be posting code for VB, when your problem is clearly posted in C#.

What you're looking for is:

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

where 'columntobedisabled' is index number of the column to be disabled...eg. 1

于 2014-09-17T12:00:30.057 回答
0

在 C# 网站或 WebForm 的情况下,在文件后面的代码中的 Page_Load() 中输入以下代码

protected void Page_Load(object sender, EventArgs e)
{
    // Your code

    ((BoundField)GridView1.Columns[columnIndex]).ReadOnly = true;
}

这样做也有助于克服错误

System.ArgumentOutOfRangeException: '指定的参数超出了有效值的范围。参数名称:索引'

于 2020-04-11T20:52:47.060 回答
-1

您需要授予"ReadOnly= true"您不喜欢编辑的列的权限。

例如。

GridView1.columns[1].ReadOnly= true;

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

于 2012-07-20T10:47:48.533 回答