Click here to Skip to main content
16,019,423 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
protected void btnGrid_Click(object sender, EventArgs e)
{
   //Displaying Textboxes data into Gridview
    dt = new DataTable();
    dt.Columns.Add("name");
    dt.Columns.Add("age is");
    string na = txtName.Text;
    int age = Convert.ToInt32(txtAge.Text);
    dt.Rows.Add(na, age);

    GridView1.DataSource = dt;
    GridView1.DataBind();


    //DataRow dr1 = dt.NewRow();
    //dr1[0] = txtName.Text;
    //dr1[1] = txtAge.Text;
    //dt.Rows.Add(dr1);
    //GridView1.DataSource = dt;
    //GridView1.DataBind();

}
Posted
Updated 2-Jul-15 3:50am
v2

Hi,
Count the Gridview Rows and then Add the Values by using the counted Rows Values
like the Below code(it C# Windows Application Code)

string dates= textBox1.Text.ToString();
int i=dataGridView1.Rows.Count;
int j=i-1;
this.dataGridView1.Rows.Add();
dataGridView1.Rows[j].Cells[0].Value=dates;
textBox1.Text="";
Thanks
 
Share this answer
 
public partial class test : System.Web.UI.Page
{
DataTable tb = new DataTable();

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
createtable();

}
protected void Button1_Click(object sender, EventArgs e)
{

tb = (DataTable)ViewState["table1"];
tb.Rows.Add(TextBox1.Text, tb.Rows.Count + 1);
GridView1.DataSource = tb;
GridView1.DataBind();

}
public void createtable()
{

tb.Columns.Add("Name", typeof(string));
tb.Columns.Add("Id", typeof(int));
ViewState["table1"] = tb;
}

}
 
Share this answer
 
public partial class test2 : System.Web.UI.Page
{
static DataTable table1 = new DataTable();

protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
Int32 Rcount =table1.Rows.Count;

if (Rcount == 0)
{
table1.Columns.Add("FName");
table1.Rows.Add(TextBox1.Text);

}
else
{
DataRow dr = table1.NewRow();
dr["FName"] = TextBox1.Text;
table1.Rows.Add(dr);

}
GridView1.DataSource = table1;
GridView1.DataBind();
}
}
 
Share this answer
 

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