如何将文本框放入DataGrid单元格

时间:2014-06-11 19:02:06

标签: c# asp.net datagrid

无论出于何种原因,当底层数据发生时,文本框的值不会改变。

这是我到目前为止所做的:

    OdbcConnection dbConnection = new OdbcConnection(connectionString);

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                dbConnection.Open();
                PopulateOPCodes();
            }
            RefreshData(DropDownList1.Items[DropDownList1.SelectedIndex].Text);
        }
        catch (Exception ex)
        {
            lblError.Text = ex.Message;
        }
    }

    protected void PopulateOPCodes()
    {
        var dataReader = new OdbcCommand("SELECT DISTINCT(OP_CD) FROM LDL_FOREMAN", dbConnection).ExecuteReader();                
        while (dataReader.Read()) DropDownList1.Items.Add(new ListItem(dataReader[0].ToString()));                
        dbConnection.Close();
    }

    protected void RefreshData(string OpCode)
    {
        //var dbCommand = new OdbcCommand("SELECT * FROM LDL_FOREMAN WHERE OP_CD = '" + OpCode + "'", dbConnection);

        var dbCommand = new OdbcCommand("SELECT * FROM LDL_FOREMAN WHERE OP_CD = ?", dbConnection);
        dbCommand.Parameters.AddWithValue("OP_CD", OpCode);


        var dataset = new DataSet();
        var dataAdapter = new OdbcDataAdapter(dbCommand);
        dataAdapter.Fill(dataset);

        GridView1.DataSource = dataset.Tables[0];
        GridView1.DataBind();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex == 5)
        {
            e.Row.Style.Add(HtmlTextWriterStyle.BackgroundColor, "Linen");
            var textBox = new TextBox {Text = e.Row.Cells[1].Text};
            e.Row.Cells[1].Controls.Add(textBox);                
        }
    }

0 个答案:

没有答案