Get hidden column value in Gridview

CSS: .hideGridColumn {display: none;}

ASP.NET: <asp:BoundField DataField = “AddressID” HeaderText = “AddressID” HeaderStyle-CssClass = “hideGridColumn” ItemStyle-CssClass = “hideGridColumn” />

C#: //Create a list of integer to store selected AddressId

List<int> lstAddressId = new List<int>();

Control chkSelect = null;

for(int iRow = 0; iRow < gvAddress.Rows.Count; iRow++){

//Find checkBox control in GridView

chkSelect = gvAddress.Rows[iRow].Cells[0].FindControl(“chkSelect”);

if(chkSelect != null){

//If CheckBox is checked then add AddressId of selected row in List by using gvAddress.Rows[iRow].Cells[1].Text property

if(((CheckBox)chkSelect).Checked){

int iAddressId = (int)gvAddress.Rows[iRow].Cells[1].Text;

lstAddressId.Add(iAddressId);

}

}

}

Anuncios

Add Rows/ Columns to GridView

Dynamically create DataTable and bind to GridView:

if(!this.IsPostBack){

DataTable dt = new DataTable();

dt.Columns.AddRange(new DataColumn[3]{new DataColumn(“Id”, typeof(int)), “Name”, typeof(string)), “Country”, typeof(string))})

dt.Rows.Add(1, “John”, “England”);

GridView1.DataSource = dt;

GridView1.DataBind();

}

*”Id”, “Name”, “Country” are Datafields in column properties.

OR

DataTable dt = new DataTable();

DataColumn dc1 = new DataColumn(“first”, typeof(string));

DataColumn dc2 = new DataColumn(“second”, typeof(string));

DataColumn dc3 = new DataColumn(“third”, typeof(string));

 

dt.Columns.Add(dc1);

dt.Columns.Add(dc2);

dt.Columns.Add(dc3);

 

for(int i = 0; i <3; i ++){     //number of rows needed

DataRow row1 = dt.NewRow();

int c = 0;

while(c < 3){

row1[c] = “some_data”;

c++;

}

dt.Rows.Add(row1);

}

 

foreach(DataColumn col in dt.Columns){

BoundField bField = new BoundField();

bField.DataField = col.ColumnName;

bField.HeaderText = col.ColumnName;

GridView1.Columns.Add(bField);

}

GridView1.DataSource = dt;

GridView1.DataBind();

 

 

Check if a column value exists in another column

SELECT ID FROM tableName WHERE Calling_ID IN (SELECT Called_ID FROM tableName)