In my webform ,I just placed one GridView and in this grid ,want to place three column ,in one column i want to place dropdownlist ,which is fill from all ids which is corresponding to first column value (portalId)
Like....| portalId |Name | DropDownList |
1 | ABC | List related with(portalId)=1|
Loading
Datta KharadPosted Dec 26, 2011, 6:08 AM
You can use following code or see in attachment.
//In source file
AutoGenerateColumns="False" onrowdatabound="GridView1_RowDataBound">
//Code file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection con;
SqlDataAdapter da;
DataSet ds = null;
string connectionString="Data Source=PC-301-18\\SQLEXPRESS;Initial Catalog=Portaldb;Integrated Security=True;Pooling=False";
protected void Page_Load(object sender, EventArgs e)
{
//if (!IsPostBack)
//{
con = new SqlConnection(connectionString);
con.Open();
Response.Write("Connection Success");
//}
}
bool isBegin = false;
protected void Button1_Click(object sender, EventArgs e)
{
GridView1.DataSource = GetDataSet().Tables[0];
GridView1.DataBind();
}
private DataSet GetDataSet()
{
string query=string.Empty;
//string query = @"SELECT DISTINCT p.portalid,p.portalname, p.portaladdress, r.roleid,r.rolename FROM tbportal p LEFT OUTER JOIN tbrolemaster r on p.portalid=r.portalid order by portalid";
//string query = @"SELECT p.portalname, p.portaladdress, p.portalid, r.roleid, r.rolename FROM tbportal p INNER JOIN tbrolemaster r on p.portalid=r.portalid";
if (!isBegin)
{
query = "SELECT p.portalid, p.portalname, p.portaladdress FROM tbportal p";
isBegin = true;
}
else
{
query = "SELECT r.portalid, r.roleid, r.rolename FROM tbrolemaster r";
}
con = new SqlConnection(connectionString);
da = new SqlDataAdapter(query, con);
ds= new DataSet();
da.Fill(ds);
return ds;
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataTable myTable = new DataTable();
DataColumn roleidCol = new DataColumn("roleid");
DataColumn rolenameCol = new DataColumn("rolename");
myTable.Columns.Add(roleidCol);
myTable.Columns.Add(rolenameCol);
DataSet ds = new DataSet();
ds = GetDataSet();
int portalid = 0;
string expression = String.Empty;
if (e.Row.RowType == DataControlRowType.DataRow)
{
portalid = Int32.Parse(e.Row.Cells[0].Text);
expression = "portalid = " + portalid;
DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1");
DataRow[] rows = ds.Tables[0].Select(expression);
foreach (DataRow row in rows)
{
DataRow newRow = myTable.NewRow();
newRow["RoleId"] = row["roleid"];
newRow["RoleName"] = row["rolename"];
myTable.Rows.Add(newRow);
}
ddl.DataSource = myTable;
ddl.DataTextField = "rolename";
ddl.DataValueField = "roleid";
ddl.DataBind();
}
}
}