ASP.NET GridView examples - Using code-behind - no wizards - no drag/drop
I am looking to implement and learn more about the ASP.NET GridView control. However, all the examples I have found seem to focus on setting up a SQL Datasource within the ASPX page. I prefer to keep markup as seperate as possible from code and therefore all datasource binding I like to do in the code-behind. So it would be great to see an example that setup the GridView in that manner.
Also, most of the examples, I have seen, step through using the Visual Studio IDE and wizards to implement the GridView. I would like to see how it is done from a pure code point of view.
Does anyone have any examples or perhaps know of some resources that describes usage of the GridView control in the manner in which 开发者_运维百科I described?
It would be great to see an example(s) that cover sorting, paging, and CRUD operations. Thanks for your time.
A simple example that includes sorting and paging. Gridview Example. Obviously you just need to plug in your call to your DAL and put the data in a DataTable. Though the example is simplistic, I think it conveys what is needed to get rolling.
MyGrid grid = new MyGrid();
grid.DataSource = GetContents(); // call into your business or data layer
grid.DataBind();
///////////////////////THIS IS CHECK ONLY////////////
protected void restore_btn_Click(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow row = GridView1.Rows[i];
bool isChecked = ((CheckBox)row.FindControl("chekbox")).Checked;
if (isChecked)
{
// Column 2 is the name column
//str.Append(GridView1.Rows[i].Cells[2].Text);
}
}
// foreach (GridViewRow row in GridView1)
//{
// for (int i = 0; i < GridView1.Rows.Count - 1; i++)
// {
// CheckBox check = (CheckBox)row.Cells[0].FindControl("chekbox");
// if (check.Checked)
// {
// //Take Row information from each column (Cell) and display it
// }
// else
// {
// //Display in seperate area
// }
//}
//string deletepatient;
//for (int i = 0; i < GridView1.Rows.Count - 1; i++)
//{
// //GridView1.SelectedRow.
// //if (GridView1.Rows[i].Cells[0]. == true)
// //{
// // deletepatient = deletepatient + "," + GridView1.Columns[i]["account_no"];
// //}
//}
// foreach(Gridviewrow gvr in Gridview1.Rows)
//{
// if(((CheckBox)gvr.findcontrol("CheckBox1")).Checked == true)
// {
// int uPrimaryid= gvr.cells["uPrimaryID"];
// }
//}
//foreach (GridView1 gvr in GridView1.Rows)
//{
// if (((CheckBox)gvr.findcontrol("checkbox1")).Checked == true)
// {
// int id = grv.cells["chekbox"];
// }
//}
ScriptManager.RegisterStartupScript(this, GetType(), "alert", "alert('Record
Restore Successfully..');", true);
}
}
精彩评论