Click here to Skip to main content
16,018,394 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted

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....

VB
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
 
Share this answer
 
Comments
tcatu 21-May-10 9:20am    
thnx for reply.
after I filled my gridview as u told , how can I get selected item and textbox.tex from gridview ?
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
 
Share this answer
 
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();

}

}
 
Share this answer
 
Comments
William Winner 19-May-10 16:30pm    
Reason for my vote of 1
what? that did nothing to answer the question.
Sandeep Mewara 21-May-10 5:47am    
Reason for my vote of 1
Code dump!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900