预填充gridview空数据源

时间:2012-08-30 21:40:55

标签: asp.net

我使用gridview空数据源进行批量插入,如何使用下拉框的值预填充实例A列?到目前为止,我在下面,但它没有工作

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        TextBox tb = (TextBox)e.Row.FindControl("YourTextBoxID");
        if(tb != null)
        {
            tb.Text = DropDownList1.SelectedItem.Text;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我不明白你对 empty DataSource的意思。我假设您实际上是指一个自动填充默认值的DataSource。

您可以使用DataTable

var tbl = new DataTable();
tbl.Columns.Add("ColumnA");
var defText = DropDownList1.SelectedItem.Text;
for(int i = 0; i < 1000; i++)
{
    tbl.Rows.Add(defText);
}
GridView1.DataSource = tbl;
GridView1.DataBind();