如何用文本框,下拉列表填充gridview动态?
问题描述:
hi如何使用文本框和复选框动态填充gridview.
例如grdvw的第一行是a,b,c
第二个是a,d,e,f
a:文本框(列)
b:下拉列表(列)
c,d,e,f ..
但是它们不是恒定的,它会根据用户选择而变化.
然后我想从gridview获取选定的项或textbox.text.
thnx为您提供帮助,
çağlarakbıyık
hi How can I fill gridview dynamically with textboxes,checkboxes.
forexample first row of grdvw is a,b,c
second one is a,d,e,f
a: textbox (column)
b:dropdownlist (column)
c,d,e,f..
But they are not constant, it changes according the user selection.
then ı want to get selected items or textbox.text from gridview.
thnx for your help ,
çağlar akbıyık
答
我们可以添加像下面的代码一样,我给出了一个简单的代码,并根据您的要求对其进行了格式化.手动为每行添加,如果网格行很低就可以(例如5).如果网格行多于该行,则您必须以另一种方式....
Hi we can add like the following code i am giving a simple code and format it as per u r requirement....but the thing is we have add manually for each row it is ok if the grid rows are low say 5....if the grid rows are more than that u have to see another way....
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If (e.Row.DataItemIndex > 0) Then
Select Case e.Row.DataItemIndex
Case 1
Dim txt As New TextBox
Dim chk As New CheckBox
GridView1.Rows(e.Row.DataItemIndex - 1).Cells(1).Controls.Add(txt)
GridView1.Rows(e.Row.DataItemIndex - 1).Cells(2).Controls.Add(chk)
Case 2
Dim ddl As New DropDownList
Dim chk As New CheckBox
GridView1.Rows(e.Row.DataItemIndex - 1).Cells(1).Controls.Add(ddl)
GridView1.Rows(e.Row.DataItemIndex - 1).Cells(2).Controls.Add(chk)
End Select
End If
End Sub
获取网格视图数据连接添加写查询从表名中选择字段名1,字段名2,其中字段名1如@ fieldname1 +''%",字段名2如@ fieldname2 +"%",按字段名1排序.然后单击确定.然后以选择数据源-f4-的形式出现selectquery-有make参数fieldname1 -controlsource,在那里选择控件控件ID textbox.text,然后确定
take a grid view make data connection add write query"selct fieldname1,fieldname2 from table name where fieldname1 like @fieldname1+ ''%'' and fieldname2 like @fieldname2+ ''%'' order by fieldname1. then click ok.then come on form select datasource-f4-selectquery-there make parameter fieldname1 -controlsource there select control control id textbox.text and then ok
在页面中创建Gridview
将GridView拖放到页面上
或
在页面中手动键入GridView定义.
Create a gridview in the page
Drag and drop the GridView on to the page
Or
Manually type GridView definition in the page.
public partial class _Default : System.Web.UI.Page
{
#region constants
const string NAME NAME ;
const string ID ID ;
#endregion
protected void Page_Load(object sender EventArgs e)
{
loadDynamicGrid();
}
private void loadDynamicGrid()
{
#region Code for preparing the DataTable
//Create an instance of DataTable
DataTable dt new DataTable();
//Create an ID column for adding to the Datatable
DataColumn dcol new DataColumn(ID typeof(System.Int32));
dcol.AutoIncrement true;
dt.Columns.Add(dcol);
//Create an ID column for adding to the Datatable
dcol new DataColumn(NAME typeof(System.String));
dt.Columns.Add(dcol);
//Now add data for dynamic columns
//As the first column is auto-increment we do not have to add any thing.
//Let''s add some data to the second column.
for (int nIndex 0; nIndex < 10; nIndex++)
{
//Create a new row
DataRow drow dt.NewRow();
//Initialize the row data.
drow[NAME] Row- + Convert.ToString((nIndex + 1));
//Add the row to the datatable.
dt.Rows.Add(drow);
}
#endregion
//Iterate through the columns of the datatable to set the data bound field dynamically.
foreach (DataColumn col in dt.Columns)
{
//Declare the bound field and allocate memory for the bound field.
BoundField bfield new BoundField();
//Initalize the DataField value.
bfield.DataField col.ColumnName;
//Initialize the HeaderText field value.
bfield.HeaderText col.ColumnName;
//Add the newly created bound field to the GridView.
GrdDynamic.Columns.Add(bfield);
}
//Initialize the DataSource
GrdDynamic.DataSource dt;
//Bind the datatable with the GridView.
GrdDynamic.DataBind();
}
}