Hi kaustubhbhat,
The GridView control renders to client is a HTML table. So we can operate it on the client side. However, please note the Gridview will render every postback, so all the change will be restored. The following code is for your reference. I hope it is helpful to you.
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
function SetNumberOfRows(textbox)
{
var number = Number(document.getElementById('Text1').value);
var table = document.getElementById('GridView1');
if (table.rows.length > number)
{
for(var i=table.rows.length; i>number;)
{
table.deleteRow(--i);
}
}
else
{
for(var i=table.rows.length; i<number; i++)
{
var row = table.insertRow();
var cell = row.insertCell();
cell.innerText = 'null';
cell = row.insertCell();
cell.innerText = 'null';
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<br />
<input id="Text1" style="position: static" type="text" />
<input id="Button1" onclick="SetNumberOfRows()" style="position: static" type="button"
value="button" /></div>
</form>
</body>
</html>
****************** code behind file
DataTable myTable;
protected void Page_Load(object sender, EventArgs e)
{
myTable = new DataTable();
myTable.Columns.Add("ID", typeof(Int32));
myTable.Columns.Add("Name", typeof(String));
myTable.Rows.Add(1,"aaa");
myTable.Rows.Add(2, "bbb");
myTable.Rows.Add(3, "ccc");
GridView1.DataSource = myTable;
GridView1.DataBind();
}
Sincerely,
Benson Yu
Microsoft Online Community Support
Please remember to click ?Mark as Answer? on the post that helps you, and to click ?Unmark as Answer? if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.