Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Use of GridView in c#

mohitkumarmohitkumar indiaPosts: 14Member
in C#

Here i am giving an ex of working with grid view in c#.

public partial class ADOdotNet_Gridviewex1 : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
using (SqlConnection cn = new SqlConnection(s))
{
using (SqlCommand cmd =
new SqlCommand("select * from Cities", cn))
{
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
gvDetails.DataSource = ds;
gvDetails.DataBind();
Session["ds"] = ds;
}
}
}
catch (SqlException ex)
{
}

}
protected void gvDetails_RowEditing(object sender, GridViewEditEventArgs e)
{
DataSet ds = (DataSet)Session["ds"];
gvDetails.EditIndex = e.NewEditIndex;
gvDetails.DataSource = ds;
gvDetails.DataBind();
}
protected void gvDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
DataSet ds = (DataSet)Session["ds"];
GridViewRow gr = gvDetails.Rows[e.RowIndex];
TableCell tc = gr.Cells[0];
int row = e.RowIndex;
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (row == i)
{
ds.Tables[0].Rows[i].Delete();
}
}
gvDetails.DataSource = ds;
gvDetails.DataBind();
}
protected void gvDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
DataSet ds = (DataSet)Session["ds"];
GridViewRow gr = gvDetails.Rows[e.RowIndex];
TableCell tc = gr.Cells[0];
int row = e.RowIndex;
TextBox tbCid = (TextBox)tc.FindControl("tbCid");
TextBox tbCityname = (TextBox)tc.FindControl("tbCityName");
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (row == i)
{
ds.Tables[0].Rows[i][0] = tbCid.Text;
ds.Tables[0].Rows[i][1] = tbCityname.Text;
}
}
gvDetails.EditIndex = -1;
gvDetails.DataSource = ds;
gvDetails.DataBind();
}
protected void gvDetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
DataSet ds = (DataSet)Session["ds"];
gvDetails.EditIndex = -1;
gvDetails.DataSource = ds;
gvDetails.DataBind();
}
protected void gvDetails_RowCreated(object sender, GridViewRowEventArgs e)
{
DataSet ds = (DataSet)Session["ds"];
if (e.Row.RowType == DataControlRowType.Header)
{
GridView HeaderGrid = (GridView)sender;
GridViewRow HeaderGridRow = new
GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
TableCell HeaderCell = new TableCell();
HeaderCell.HorizontalAlign = HorizontalAlign.Center;
HeaderCell.Text = "CitiesDetails";
HeaderCell.ColumnSpan = 8;
HeaderGridRow.Cells.Add(HeaderCell);
gvDetails.Controls[0].Controls.AddAt(0, HeaderGridRow);
}

}

}

For preparing interview in c# you can refer this link c# interview questions

Sign In or Register to comment.