Clearing Datagrid on Button click
I want that when I click button,datagrid got cleared. So How can I do that
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Manoj SevdaPosted Nov 16, 2011, 6:29 AM
If(ds.Tables[0].Rows>0)
{
dataGridView1.DataSource = ds;
dataGridView1.DataBind();
}
else
{
dataGridView1.DataSource = null;
dataGridView1.DataBind();
}
Satyapriya NayakPosted Nov 15, 2011, 3:28 AM
Try this...
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class Default2 : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand com;
SqlDataAdapter sqlda;
DataSet ds;
string str;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
void bind()
{
SqlConnection con = new SqlConnection(connStr);
try
{
con.Open();
str = "select * from employee";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds, "employee");
DataGrid2.DataSource = ds;
DataGrid2.DataMember = "employee";
DataGrid2.DataBind();
con.Close();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
protected void Btn_Clear(object sender, EventArgs e)
{
if (DataGrid2 != null)
{
DataGrid2.DataSource = null;
DataGrid2.DataMember = null;
DataGrid2.DataBind();
}
}
}
Thanks
If this post helps you mark it as answer
Datta KharadPosted Nov 15, 2011, 2:32 AM
private void button4_Click(object sender, EventArgs e)
{
if (dataGrid1 != null)
{
dataGrid1 .DataSource = null;
}
}
Priya LingePosted Nov 15, 2011, 2:10 AM
1. Fill Datagrid :
public void getdata()
{
sqlConnection.Open();
string query = "select * from EmplyoeeData";
sqlCommand = new SqlCommand(query, sqlConnection);
da = new SqlDataAdapter(sqlCommand);
ds = new DataSet();
da.Fill(ds);
//sqlCommand.ExecuteNonQuery();
dataGridView1.DataSource = ds.Tables[0];
dataGridView1.Show();
}
public Form2()
{
InitializeComponent();
getdata();
}
2.Clear Datagrid
private void button1_Click(object sender, EventArgs e)
{
dataGridView1.DataSource = null;
dataGridView1.Show();
}
Hope this will help you.
Thanks.
TulasiPosted Nov 15, 2011, 2:03 AM
put this line
dataGridView1.DataSource = null;