update gridview

A

aarif.shah

Hi, i am Aarif,

I Have a gridview.On clicking the edit link of gridview i want my
edititem template(that i have defined ) to show either Texbox control
or dropdown list depending on some condition.
when user clicks on update link of the gridview,the value shoud be
taken from either textbox or selected value of dropdown list
depending upon which control is appearing in the edit mode.

How can i do this?

Aarif Majid
 
R

Riki

I suggest putting both the TextBox and the DropDownlist on the edit
template.
Depending on the condition, give the right one the property Visible=False.
You can even do this through databinding:
Visible='<%# Eval("somefield") %>'

On update, check which one is visible, and retrieve its value.
 
Joined
Jul 13, 2007
Messages
2
Reaction score
0
Gridview Editing

Hi, i am Aarif,

I Have a gridview.On clicking the edit link of gridview i want my
edititem template(that i have defined ) to show either Texbox control
or dropdown list depending on some condition.
when user clicks on update link of the gridview,the value shoud be
taken from either textbox or selected value of dropdown list
depending upon which control is appearing in the edit mode.

How can i do this?

Aarif Majid

Try This

int index;
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{


index = e.NewEditIndex;
GridView1.EditIndex = e.NewEditIndex;
txtId.Text = ds.Tables[0].Rows[index][0].ToString();
txtName.Text = ds.Tables[0].Rows[index][1].ToString();
txtSalary.Text = ds.Tables[0].Rows[index][2].ToString();

}


protected void btnInsert_Click(object sender, EventArgs e)
{

}

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int id,salary;
string name;


id = int.Parse(txtId.Text);
name = txtName.Text;
salary= int.Parse(txtSalary.Text);
SqlConnection cnn = new SqlConnection("Data Source=AST345\\SQLEXPRESS;Initial Catalog=test;Integrated Security=True");
cnn.Open();
SqlCommand cmd = new SqlCommand("update Employee set name=@name,salary=@salary where id=@id", cnn);
cmd.Parameters.Add(new SqlParameter("@name",name));
cmd.Parameters.Add(new SqlParameter("@salary",salary));
cmd.Parameters.Add(new SqlParameter("@id", id));
cmd.ExecuteNonQuery();
cnn.Close();

GridView1.EditIndex = -1;

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,120
Latest member
ShelaWalli
Top