日期:2014-05-19  浏览次数:20414 次

继续追问
在datagrid中用到dropdownlist,当我想修改一行数据时。
怎么操作可以使对dropdownlist的操作反映到数据库

http://community.csdn.net/Expert/topic/5496/5496490.xml?temp=.1722986

------解决方案--------------------
dropdownlist会有:AutoPostBack=true 这样就会有后台事件了,都指定一个事件就应该可以了。
------解决方案--------------------
你要给DropDownList添加事件,如楼上说设为true
,回发到服务器之后,这个事件被DataGrid拦截,成为它的事件像 OnItemCommand这种
它会告诉你这个事件发生在哪一行,哪一列,然后,你得知,便可以写代码了
------解决方案--------------------
private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string str = ((DropDownList)(e.Item.FindControl( "DropDownList1 "))).SelectedItem.Value;
string strSql = "update 表 set 字段 = ' " + str + " ' where 你的条件 ";
ExecuteSql(strSql); //执行你的update语句
dgShow.EditItemIndex = -1;
BindData(); //重新绑顶
}
------解决方案--------------------
或者不用datagrid自带的update,自己在右边添加表中相应字段的控件修改也行。

比如你要修改datagrid的dropdownlist控件里值到数据库,你可以在datagrid里留个选择,然后在右边加个dropdownlist控件,[假设它id为dpdName]

public System.Data.SqlClient.SqlConnection mySqlConnection;
public System.Data.SqlClient.SqlCommand mySqlCommand;
public System.Data.SqlClient.SqlDataAdapter mySqlDataAdapter;
public System.Data.SqlClient.SqlDataReader mySqlDataReader;
public System.Data.DataSet myDataSet;
public System.Data.DataView myDataView;

//数据库连接
public static SqlConnection GetConn()
{
this.mySqlConnection = new SqlConnection( "Data Source=.;Initial Catalog=数据库名;User ID=sa;Password= ");
return mySqlConnection;
}

//修改数据
protected static void Update(string 你dropdownlist的字段名)
{
this.mySqlConnection = GetConn();
this.mySqlCommand = mySqlConnection.CreateCommand();
mySqlConnection.Open();
mySqlCommand.CommandText = "update 表名 set 你dropdownlist的字段名 = ' " + 你dropdownlist的字段名 + " ' where 你的条件 ";
mySqlCommand.ExecuteNonQuery();
mySqlConnection.Dispose();
mySqlConnection.Close();
}

//数据修改
protected void btnEdit_Click(object sender, EventArgs e)
{
string 你dropdownlist的字段名 = dpdName.SelectedValue.ToString();

你的Inherits名.Update(你dropdownlist的字段名);
}