dynamic datagrid rows

Posted by Swapnali under ASP.NET on 10/5/2012 | Points: 10 | Views : 945 | Status : [Member] | Replies : 3
There is one textbox to enter no of rows required for datagrid and one create button
when submit that no of rows must be displayed for the datagrid




Responses

Posted by: Jayakumars on: 10/6/2012 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi

swapnil try this

protected void Button1_Click(object sender, EventArgs e)

{

dt.Columns.Add("Name");

DataRow dr = dt.NewRow();

dr[0] = "Jesus";

dt.Rows.Add(dr);



BoundField BFiled1 = new BoundField();

BFiled1.HeaderText = "Name";

BFiled1.DataField = "Name";

grd1.Columns.Add(BFiled1);



grd1.DataSource = dt;

grd1.DataBind();

}



Mark as Answer if its helpful to you

Swapnali, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sureshknkt7 on: 10/6/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,

this code will help you,

In Aspx Page:

<asp:TextBox ID="txtCount" runat="server"></asp:TextBox>

<asp:Button ID="btnSubmit" runat="server" Text="GetRecords" OnClick="GetRecords" />
<br>
<asp:DataGrid ID="datagrid1" runat="server" AllowSorting="true" AutoGenerateColumns="false">
</asp:DataGrid>


In Code Behind: :

Put below code on button click event: and notice that
whatever whatever value given in the textbox that value
assigned to datagrid pagesize property.


 protected  void GetRecords(object sender,EventArgs e)

{
datagrid1.PageSize = int.Parse(txtCount.Text.ToString());
DataTable table = new DataTable();
using (SqlConnection con = new SqlConnection(_connStr))
{
string sqlquery = "select *from Login";
using (SqlCommand cmd = new SqlCommand(sqlquery, con))
{
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(table);
}
datagrid1.DataSource = table;
datagrid1.DataBind();
}
}


Thank you

Swapnali, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kianurieves on: 10/9/2012 [Member] Starter | Points: 25

Up
0
Down
After binding datagrid to itemssouce , you will find one event called autogeneratingcolumn, in this event catch the column with the help of the header text and load datatemplate from resources.
http://www.dapfor.com/en/net-suite/net-grid/tutorial/real-time-blotter


Swapnali, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response