T

T

  • NA
  • 5
  • 0

making a column invisible in a datagrid

Aug 16 2006 6:32 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.
//Form1.aspx.cs 

private void Search_Click(object sender, System.EventArgs e)
{ Class1 cl = new Class1();
   request="search";
   DataSet MyDataSet = cl.search(MyForm,sender,Page,request);
}
//Class1.cs
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 ;
}

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 get an exception coz the item_Bound() event gets called even before the dataset is returned to the calling function. i cannot compromise on this approach ie. the way the functions are called & controls passed from 1 to another. Also, the # of columns that would be present also depends on the query which gets generated dynamically, so I cannot set "AutoGenerateColumns = False".
But I need to fetch the pkey or the unique id of the record on which the user clicks from the result displayed in the datagrid.
can anyone help me with this...????? PLz??????







Answers (1)