Posted by Unknown on 1:14 AM
Labels:

Filling a Dropdownlist inside a gridview with database values:

EG:
protected void gdLabHourSetting_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddlSession = (DropDownList)e.Row.FindControl("ddlSession");

if (ddlSession != null)
{
ddlSession.DataSource = tblLabSession;
ddlSession.SelectedIndex = ddlSession.SelectedIndex < lblsession =" ((Label)(gdLabHourSetting.Rows[e.NewEditIndex].Cells[1].FindControl(" editindex =" e.NewEditIndex;" ddlsession =" ((DropDownList)(gdLabHourSetting.Rows[e.NewEditIndex].Cells[1].FindControl(" datasource =" tblLabSession;" datatextfield =" tblLabSession.Columns[" datavaluefield =" tblLabSession.Columns[" idx =" ddlSession.Items.IndexOf(ddlSession.Items.FindByText(lblSession.Text.Trim()));" selectedindex =" ddlSession.Items.IndexOf(ddlSession.Items.FindByText(lblSession.Text.Trim()));" style="font-weight: bold;">Adding data through gridview into DB:

Make the command field as a template column and add a link button to its footer template:

Eg:

private void FillCustomerInGrid()
{
DataTable dtCustomer= customer.Fetch();

if (dtCustomer.Rows.Count>0)
{
GridView1.DataSource = dtCustomer;
GridView1.DataBind();
}
else
{
dtCustomer.Rows.Add(dtCustomer.NewRow());
GridView1.DataSource = dtCustomer;
GridView1.DataBind();

int TotalColumns = GridView1.Rows[0].Cells.Count;
GridView1.Rows[0].Cells.Clear();
GridView1.Rows[0].Cells.Add(new TableCell());
GridView1.Rows[0].Cells[0].ColumnSpan = TotalColumns;
GridView1.Rows[0].Cells[0].Text = "No Record Found";
}
}

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("AddNew"))
{
TextBox txtNewName=(TextBox)GridView1.FooterRow.FindControl("txtNewName");
DropDownList cmbNewGender = (DropDownList)GridView1.FooterRow.FindControl("cmbNewGender");
TextBox txtNewCity = (TextBox)GridView1.FooterRow.FindControl("txtNewCity");
TextBox txtNewState = (TextBox)GridView1.FooterRow.FindControl("txtNewState");
DropDownList cmbNewType = (DropDownList)GridView1.FooterRow.FindControl("cmbNewType");

customer.Insert(txtNewName.Text, cmbNewGender.SelectedValue, txtNewCity.Text, txtNewState.Text, cmbNewType.SelectedValue) ;
FillCustomerInGrid();
}
}

0 comments:

Post a Comment