melissa820
02-24-2006, 09:20 PM
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:
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!
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:
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!