2
Answers

How to pass autogenerate gridview column names in view states

arun kumar

arun kumar

12y
2.5k
1
How to pass autogenerate gridview column names in view states??? Number of Columns may be vary depending on table Column.... 
I am Loading Dropdownlist with various table names depending on database.... 
On selecting particular table grid view will be generated... And Column names may be vary depending on Table.... 
Now I want to get All Column Names in View State... How to get that??? 
//This Populate Grid View will generate gridview for any Table which is selected

private void PopulateGridView() { 
  try {
  SqlConnection con;
  con = new SqlConnection(@"Data Source=CKQJ8433\SQLEXPRESS;" + "Initial Catalog" + "=" +drddatabase.SelectedValue + ";" + "Integrated Security=True");
  SqlCommand cmd = new SqlCommand();
  cmd.CommandText = "select * from " + drdtablename.SelectedValue;
  cmd.CommandType = CommandType.Text;
  cmd.Connection = con;
  SqlDataAdapter da = new SqlDataAdapter();
  da.SelectCommand = cmd;
  DataSet ds = new DataSet();
  da.Fill(ds, "Disp");
  grdview.DataSource = ds.Tables["disp"];
  Label1.Text = " ";
  //View State to Delete Data From Gridview
  ViewState["arun"] = ds;
  string strColumnsName = ds.Tables[0].Columns[0].ColumnName;
  //ViewState to delete Data From Database
  ViewState["id"] = strColumnsName;
  }
  catch (SqlException ex)
  {
  }
  grdview.DataBind();
  }
  protected void grdview_RowEditing(object sender, GridViewEditEventArgs e)
  {
  //This is Data Set Which I got From PopulateGridView
  DataSet dt = (DataSet)ViewState["arun"];
  //In Employee I am Having 3 Columns... So I write this code
  string colname1= dt.Tables[0].Columns[0].ColumnName;
  ViewState["eid"] = colname1;
  string colname2 = dt.Tables[0].Columns[1].ColumnName;
  ViewState["ename"] = colname2;
  string colname3 = dt.Tables[0].Columns[2].ColumnName;
  ViewState["ecity"] = colname3;
  grdview.EditIndex = e.NewEditIndex;
  PopulateGridView();
  }

This For Particular Employee Table which consist of 3 Columns.... 
For Different Table Columns may be vary....
Answers (2)
0
Roei Bar
NA 7.8k 0 15y

enjoy

class Program
{
static void Main(string[] args)
{
Microsoft.Win32.
RegistryKey subKey =
Microsoft.Win32.
Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Office");
if (checkIfKeyExists(subKey))
{
subKey =
Microsoft.Win32.
Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Office\\11.0");
if (checkIfKeyExists(subKey))
{
subKey =
Microsoft.Win32.
Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Office\\11.0\\Outlook\\Options");
if (!checkIfKeyExists(subKey))
{
Microsoft.Win32.
Registry.LocalMachine.CreateSubKey("Software\\Microsoft\\Office\\11.0\\Outlook\\Options");
}
subKey =
Microsoft.Win32.
Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Office\\11.0\\Outlook\\Options\\Mail");
if (!checkIfKeyExists(subKey))
{
Microsoft.Win32.
Registry.LocalMachine.CreateSubKey("Software\\Microsoft\\Office\\11.0\\Outlook\\Options\\Mail");
}
 
Microsoft.Win32.
Registry.LocalMachine.SetValue("AllowTNEFtoCreateProps", "00000000", RegistryValueKind.DWord);
Microsoft.Win32.
Registry.LocalMachine.SetValue("AllowMSGFilestoCreateProps", "00000001", RegistryValueKind.DWord);
}
}
}
private static bool checkIfKeyExists(Microsoft.Win32.RegistryKey subKey)
{
bool status = true;
if (subKey == null)
{
status =
false;
}
return status;
}
}
Accepted
0
faraz akram
NA 154 0 15y
I have a requirement of following data representation on 1 page on a rdlc report SR# Part# SR# Part# 1 ## 51 ## 2 ## 52 ## . ## . ## . ## . ## 50 ## 100 ## i can easily manage if their is only one table of data but as per above requirement one page must contains atleast 100 rows, 50 on left hand side and 50 on right hand side any help regading this thanx in advance
0
faraz akram
NA 154 0 15y
thanks a tons it really works .... :))