Gridview operations inside datalist
this deals with using a gridview inside a datalist
Designing
First Drag and drop a datalist and specify its datakeyfield property to State
The DataKeyfield is used allow users to edit items in a DataList control.
Inside the Item Template Section place a gridview control and a label to display the StateCode Behind
Bind Datalist
Follow the usual procedure to bind the datalist
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection Con = new SqlConnection(Connection);
Con.Open();
string sql = "Select distinct State from CustomerCust";
Da = new SqlDataAdapter(sql, Con);
DataTable dt = new DataTable();
Da.Fill(dt);
DataList1.DataSource = dt;
DataList1.DataBind();
Con.Close();
}
}
Inorder to use the gridview inside the datalist bind the grind inside the ItemDataBound Event of DataList control
private void BindGrid(GridView GridView, string CusState)
{
SqlConnection Con = new SqlConnection(Connection);
Con.Open();
string Sql = "Select Cus_No,Cus_Name,Cus_Gender,Cus_Age,Cus_City,Cus_State,Cus_Code from CustomerCust where Cus_State ='" + CusState + "'";
Da = new SqlDataAdapter(Sql,Con);
DataTable dt = new DataTable();
Da.Fill(dt);
GridView.DataSource = dt;
GridView.DataBind();
}ItemDataBound
protected void DataList1_ItemDataBound
(object sender, DataListItemEventArgs e)
{
GridView GridView1=(GridView)e.Item.FindControl("GridView1");
BindGrid(GridView1,DataList1.DataKeys[e.Item.ItemIndex].ToString());
}GridView Operations
RowEditing:
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView GridView1 = (GridView)sender;
GridView1.EditIndex = e.NewEditIndex;
BindGrid(GridView1, GridView1.DataKeys[e.NewEditIndex][0].ToString());
}
* The above code is used to create an object from the sender and identifies the control firing the edit event
* Assign the EditIndex from the GridView events NewEditIndex.
* Call the BindGrid function by passing the created GridView object and the State value taken from the DataKeyNames property
below comes the usual procedure
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView GridView1 = (GridView)sender;
GridView1.EditIndex = -1;
BindGrid(GridView1, GridView1.DataKeys[e.RowIndex][0].ToString());
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridView GridView1 = (GridView)sender;
TextBox TxCusName = (TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0];
TextBox TxCusGender = (TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0];
TextBox TxtCusAge = (TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0];
TextBox TxCusCity = (TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0];
string sql = "Update CustomerCust set No=@No,Name=@Name,Gender=@Gender,Age=@Age,City=@City where Cus_No=@Cus_No";
//Update CustomerCust set No=@No,Name=@Name,Gender=@Cus_Gender,Age=@Age,City=@City where No=@No
SqlConnection conn = new SqlConnection(Connection);
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.Add("@Cus_No", SqlDbType.Int).Value
= GridView1.DataKeys[e.RowIndex].Values[1].ToString();
cmd.Parameters.Add("@Name", SqlDbType.VarChar, 50).Value = TxCusName.Text;
cmd.Parameters.Add("@Gender", SqlDbType.VarChar, 6).Value = TxCusGender.Text;
cmd.Parameters.Add("@Age", SqlDbType.Int).Value = TxtCusAge.Text;
cmd.Parameters.Add("@City", SqlDbType.VarChar, 50).Value = TxCusCity.Text;
cmd.Prepare();
cmd.ExecuteNonQuery();
conn.Close();
GridView1.EditIndex = -1;
BindGrid(GridView1, GridView1.DataKeys[e.RowIndex][0].ToString());
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//Part 1
GridView GridView1 = (GridView)sender;
DataListItem dlItem = (DataListItem)GridView1.Parent;
DataListItemEventArgs dle = new DataListItemEventArgs(dlItem);
if (e.Row.RowType == DataControlRowType.Header)
{
CheckBox chkSelectAll = (CheckBox)e.Row.FindControl("chkSelectAll");
chkSelectAll.Attributes.Add("onclick",
"fnSelectAll('" + chkSelectAll.ClientID + "', '"
+ DataList1.DataKeys[dle.Item.ItemIndex].ToString() + "');");
}
//Part 2
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[6].HasControls())
{
LinkButton lnkbtnDelete = ((LinkButton)e.Row.Cells[6].Controls[0]);
lnkbtnDelete.Attributes.Add("onclick", "return confirm('Do you want to Delete?');");
}
}
}
This is a javascript for selectall
function fnSelectAll(chkname, cusstate) {
var inputs = document.getElementsByTagName('input');
var checked = false;
var hidCusState;
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].id == chkname && inputs[i].type == "checkbox" && inputs[i].checked == true) {
checked = true;
break;
}
}
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].id != chkname && inputs[i].type == "checkbox") {
hidCusState = inputs[i].id.replace("chkCusNo", "hidCusState");
if (document.getElementById(hidCusState).value == cusstate)
inputs[i].checked = checked;
}
}
}
Use the Row DataBound to use the SelectAll
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//Part 1
GridView GridView1 = (GridView)sender;
DataListItem dlItem = (DataListItem)GridView1.Parent;
DataListItemEventArgs dle = new DataListItemEventArgs(dlItem);
if (e.Row.RowType == DataControlRowType.Header)
{
CheckBox chkSelectAll = (CheckBox)e.Row.FindControl("chkSelectAll");
chkSelectAll.Attributes.Add("onclick",
"fnSelectAll('" + chkSelectAll.ClientID + "', '"
+ DataList1.DataKeys[dle.Item.ItemIndex].ToString() + "');");
}
//Part 2
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[6].HasControls())
{
LinkButton lnkbtnDelete = ((LinkButton)e.Row.Cells[6].Controls[0]);
lnkbtnDelete.Attributes.Add("onclick", "return confirm('Do you want to Delete?');");
}
}
}