Make ASP.Net GridView Column (Cell) non editable (ReadOnly)

Last Reply one year ago By chetan

Posted one year ago

i wanted to update some records from gridview row.

So Amount and Percentage should be noneditable and rest all fields should be editable after clicking on edit linkbutton.How to do it? This is my code for updating.

    protected void gvDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
       // TextBox txtam = (TextBox)gvDetails.FooterRow.FindControl("txtfamtt");
       // TextBox txtper = (TextBox)gvDetails.FooterRow.FindControl("txtfpercentage");
        TextBox txtconf = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtconfirm");
        TextBox txtdiff = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtdifference");
        RadioButtonList rdott = (RadioButtonList)gvDetails.Rows[e.RowIndex].FindControl("rdottstatus");
        RadioButtonList rdoconfirm = (RadioButtonList)gvDetails.Rows[e.RowIndex].FindControl("rdoConfirmation");
        Int64 currInvoiceId = Convert.ToInt64(Request.QueryString["edit_invoice_id"]);

        try
        {

            MySqlConnection connect;
            string connectStr = ConfigurationManager.ConnectionStrings["ExpoCrmConnectionString"].ToString();
            connect = new MySqlConnection(connectStr);
           // string queryStr = "insert into payment_receipt_status(invoic_id,Amount,Percentage,TTStatus,confirmationstatus,confirmationamount,Differenceamount)values(?iid,?Amount,?Percentage,?TTStatus,?confirmationstatus,?confirmationamount,?Differenceamount)";
            string queryStr="update payment_receipt_status set TTStatus=?TTStatus,confirmationstatus=?confirmationstatus,confirmationamount=?confirmationamount,Differenceamount=?Differenceamount where invoic_id=?iid");
            connect.Open();
            MySqlCommand command = new MySqlCommand(queryStr, connect);
            command.Parameters.AddWithValue("?iid", currInvoiceId);
            //command.Parameters.AddWithValue("?Amount", txtam.Text);
           // command.Parameters.AddWithValue("?Percentage", txtper.Text);
            command.Parameters.AddWithValue("?TTStatus", rdott.SelectedItem.Text);
            command.Parameters.AddWithValue("?confirmationstatus", rdoconfirm.SelectedItem.Text);
            command.Parameters.AddWithValue("?confirmationamount", txtconf.Text);
            command.Parameters.AddWithValue("?Differenceamount", txtdiff.Text);
            command.ExecuteNonQuery();
            connect.Close();
            FillVendorGrid();
            //txtam.Text = "";
           // txtper.Text = "";
            txtconf.Text = "";
            txtdiff.Text = "";
            connect.Close();
        }
        catch (Exception ex)
        {

        }
    }
Posted one year ago

Hi chetan,

You can use BoundField and set ReadOnly property to True for columns.


Posted one year ago

hi sir,

i have used template field. i have skipped edititemtemplate for Amount and Percentage.

                                                   <asp:TemplateField HeaderText="Amount" SortExpression="Amount">
                                                       <ItemTemplate>
                                                           <asp:Label ID="Label1" runat="server" Text='<%# Bind("Amount") %>'></asp:Label>
                                                       </ItemTemplate>
                                                       <FooterTemplate>
                                                           <asp:TextBox ID="txtfamtt" runat="server"  Width="100px"></asp:TextBox>
                                                       </FooterTemplate>
                                                   </asp:TemplateField>

                                                   <asp:TemplateField HeaderText="Percentage" SortExpression="Percentage">
                                                      
                                                       <ItemTemplate>
                                                           <asp:Label ID="Label2" runat="server" Text='<%# Bind("Percentage") %>'></asp:Label>
                                                       </ItemTemplate>
                                                       <FooterTemplate>
                                                           <asp:TextBox ID="txtfpercentage" runat="server"  Width="100px"></asp:TextBox>
                                                       </FooterTemplate>
                                                   </asp:TemplateField>