Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Oct 2004
    Posts
    64
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Datagrid inline editing problem

    I have a datagrid for which the columns are populated at runtime.

    I want to be able to do inline editing with this datagrid. So I added a column with an Edit button, and have coded all the edit/update/cancel methods. I only have one problem.

    When updating the database, I need the field's ID (the primary key) in order to know which row to update. But when I edit the datagrid the ID field is editable too. I obviously don't want users to be able to change this field. How can I make this field read-only?

    I've attached some of the code and a screenshot of the problem:

    Code:
    private void dgResults_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    	string pid = ((TextBox)e.Item.Cells[1].Controls[0]).Text;
    	string computerID = ((TextBox)e.Item.Cells[2].Controls[0]).Text;
    	string firstName = ((TextBox)e.Item.Cells[3].Controls[0]).Text;
    	string lastName = ((TextBox)e.Item.Cells[4].Controls[0]).Text;
    	
    	string query = "UPDATE CompName SET " + 
    		"Computer_ID = '" + computerID + "', " + 
    		"FirstName = '" + firstName + "', " + 
    		"LastName = '" + lastName + 
    		"WHERE ID = @pID";
    			
    	string connString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
    
    	SqlConnection conn = new SqlConnection(connString);
    	conn.Open();
    	SqlCommand updateCommand = new SqlCommand(query, conn);
    	SqlParameter paramID = new SqlParameter("@pID", pid);
    	updateCommand.Parameters.Add(paramID);
    
    	try 
    	{
    		updateCommand.ExecuteNonQuery();
    	}
    	catch (Exception ex) 
    	{
    		lblResults.Text = "Error:  " + ex.Message;
    	}
    	finally 
    	{
    		updateCommand.Connection.Close();
    		lblSearchField.Text = "ID";
    		lblSearchValue.Text = pid;
    	}
    	dgResults.EditItemIndex = -1;
    	Bind();
    }
    
    private void Bind() 
    {
    	string myQuery = "SELECT ID, FirstName, LastName FROM CompName WHERE " + 
    			lblSearchField.Text + " LIKE '%" + 
    			lblSearchValue.Text + "%'"; 
    			
    	if (lblSortBy.Text != "") myQuery += " ORDER BY " + lblSortBy.Text;
    			
    	string connString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
    			
    	try 
    	{
    		myDataAdapter = new SqlDataAdapter(myQuery, connString);
    		myDataAdapter.Fill(myDataSet, "CompName");
    		dgResults.DataSource = myDataSet.Tables["CompName"];
    		dgResults.DataBind();
    	}
    	catch (Exception ex) 
    	{
    		this.lblResults.Text = "There was an error searching the database.  " + ex.Message;
    	}
    }
    Thanks in advance!
    Attached Thumbnails Attached Thumbnails Datagrid inline editing problem-screenshot.gif  

  • #2
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Can you post your html of your datagrid?

    Thanks

  • #3
    New Coder
    Join Date
    Oct 2004
    Posts
    64
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I'm not at work anymore, but I'll post it first thing Monday morning. Thanks for trying to help!

  • #4
    $object->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Jun 2003
    Posts
    3,073
    Thanks
    2
    Thanked 22 Times in 22 Posts
    You just need to add one additional property to your column type, for example:

    Code:
    <asp:BoundColumn
               DataField="DataSourceField"
               DataFormatString="FormatString"
               FooterText="FooterText"
               HeaderImageUrl="url"
               HeaderText="HeaderText"
               ReadOnly="True|False"
               SortExpression ="DataSourceFieldToSortBy"
               Visible="True|False"
               FooterStyle-property="value"
               HeaderStyle-property="value"
               ItemStyle-property="value"/>
    That bolded property would of course be ReadOnly=True for your situation. Also, note that the property is the same, regardless of the column type in your HTML.
    Last edited by vinyl-junkie; 02-25-2006 at 07:16 AM.
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    SNAP to it!

  • #5
    New Coder
    Join Date
    Oct 2004
    Posts
    64
    Thanks
    2
    Thanked 0 Times in 0 Posts
    My HTML doesn't have that in it. It looks like:

    Code:
    <asp:DataGrid id="dgResults" runat="server"
       AutoGenerateColumns="True">
            
       <Columns>
        <asp:EditCommandColumn EditText="Edit" 
              ButtonType="LinkButton"
              UpdateText="Update" CancelText="Cancel" />                  
       </Columns>        
    </asp:DataGrid>
    I tried adding the following code into my HTML, but then I ended up with 2 columns called "ID". One that was read only and one that was editable.

    Code:
     <asp:BoundColumn HeaderText="ID" DataField="ID"
                   ReadOnly="True" />
    Last edited by melissa820; 02-26-2006 at 12:22 AM.

  • #6
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Thats because you have AutoGenerateColumns set to true. This will create all the columns for you. So in that case, it's creating them for you, and then you're creating a readonly one. I've never used this, I mostly always have template columns for my data, therefore I can have more control over it.

    I'll show you an example of a column of one of my datagrids where I don't allow it to be edited:
    Code:
    <Columns>
    	<asp:TemplateColumn HeaderText="Client">
    		<ItemTemplate>
    			<asp:Label id=lblClientrunat="server" Text='<%# DataBinder.Eval(Container, "DataItem.ClientName") %>'>
    			</asp:Label>
    		</ItemTemplate>
    	</asp:TemplateColumn>
    
    ... more columns below
    </Columns>
    Now, one of my columns that I do allow editing on, I create a textbox in the <EditItemTemplate>
    Code:
    <asp:TemplateColumn HeaderText="Mon">
    	<ItemTemplate>
    		<asp:Label id=Label3 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Mon") %>'>
    		</asp:Label>
    	</ItemTemplate>
    	<EditItemTemplate>
    		<asp:TextBox id=txtEditMon Text='<%# DataBinder.Eval(Container, "DataItem.Mon") %>' Columns="2" Runat="server">
    		</asp:TextBox>
    	</EditItemTemplate>
    </asp:TemplateColumn>
    I either recomend you use bound columns or use template columns, as with the columns automatically generated you cannot have control over such things.

    Using bound columns or template columns is not difficult at all.
    Let us know how it goes.

    Good luck;

  • #7
    New Coder
    Join Date
    Oct 2004
    Posts
    64
    Thanks
    2
    Thanked 0 Times in 0 Posts
    This worked perfectly, thank you!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •