Search code examples
c#sqlasp.netgridviewdatafield

Edit two Columns in GridView keeping other Columns' data intact


I have GrideView in my asp.net project and i want to edit only two columns , i used Read-Only="true" property but the problem was after editing the two columns the data of others become empty !!!! Please help me how to avoid this problem.

<Columns>
   <asp:HyperLinkField DataNavigateUrlFields="RNum" DataNavigateUrlFormatString="WebForm2.aspx?RNum={0}" DataTextField="RNum" HeaderText="No" />
   <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" ReadOnly="true"/>
   <asp:BoundField DataField="AssignTo" HeaderText="AssignTo" SortExpression="AssignTo" />
   <asp:BoundField DataField="Priority"   HeaderText="Priority" SortExpression="Priority" ReadOnly="true" />
   <asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" />
   <asp:BoundField DataField="UserName"   HeaderText="Requested By" SortExpression="UserName" ReadOnly="true" />
   <asp:BoundField DataField="ddate"  HeaderText="Date" SortExpression="ddate" ReadOnly="true" />
</Columns>

Solution

  • You have to modify the underlying SqlDataSource Update command property, like in the following example:

    UpdateCommand="Update Employees SET FirstName=@FirstName,LastName=@LastName,Title=@Title WHERE EmployeeID=@EmployeeID"
    

    (re: https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.updatecommand(v=vs.110).aspx)