T

T

  • NA
  • 5
  • 0

making a column invisible in a datagrid

Aug 16 2006 6:25 AM
Hi,
I am developing a web application in asp.net using C#. I have a form which has a search button. This search results in a list of all the customers. I have a method which is called on the click event of search. This method belongs to a separate class actually generates the select statement & connects to the database & then displays the result in a datagrid. So, I have the dataset created in a class which is different from the one from which the "search" button was clicked.
I need to hide the primary key column of the datagrid. I tried writing the code given below in the onItemBound event of the datagrid in which the results are displayed.

public void Item_Bound(Object sender, DataGridItemEventArgs e)

{

DataView dv = new DataView(MyDataSet.Tables["myTable"]) ;

DataColumnCollection dc =dv.Table.Columns;

e.Item.Cells[dc.IndexOf(dc["pkey"])].Visible = false;

}

But, I have a PROBLEM here....
1) This event handler in written in Form1.aspx.cs file as the datagrid is placed in the Form1.aspx. But my onclik of search event handler calls a method which is present in another class.

private void Search_Click(object sender, System.EventArgs e)

{ Class1 cl = new Class1();
   request="search";

DataSet MyDataSet = cl.search(MyForm,sender,Page,request);

}

public class Class1
{
DataSet dataset;

public Class1(){}

public DataSet search(HtmlForm form,Object sender,Page Page, string requestType)
{
   string query="select firstname, lastname from customers";
   dataset = displayresult()
   return dataset;
}

public DataSet displayResults(Page page, System.Web.UI.HtmlControls.HtmlForm form){
SqlDataAdapter sqladap = new SqlDataAdapter (query,connstring);
DataSet dst = new DataSet();
sqladap.Fill(dst,"customer");
datagrid_1.DataSource=dst; //datagrid_1 refers to the control on the Form1.aspx page in which we need to display the result
datagrid_1.DataBind();


return dst ;
}







Answers (3)