I need to build a datagrid with using the information that was used in from a dropdownlist.
I start of with a dropdownlist of code types. The user selects a code type and presses the button
and we want to use the index of the dropdownlist (CODE_TYPE_ID) to build the datagrid.
The select will be getting codes for a specific code type. SELECT CODE_DESC WHERE CODE_TYPE_ID = SELECTEDINDEX
This will be need to build the datagrid. Except I have problems getting started.
Here is the code: How should I go to modify the code?
public class codestypes : System.Web.UI.Page
{
protected System.Web.UI.WebControls.
Label lblFullName;
protected System.Web.UI.WebControls.
TextBox newCountry;
protected System.Web.UI.WebControls.
TextBox newName;
protected System.Web.UI.WebControls.
DataGrid DataGrid1;
protected System.Web.UI.WebControls.
Label pathTitle;
protected System.Web.UI.WebControls.
Label typeName;
protected System.Web.UI.WebControls.
DropDownLi
st DropDownList1;
protected System.Web.UI.WebControls.
Button Button1;
protected System.Web.UI.WebControls.
Button addCountry;
private void Page_Load(object sender, System.EventArgs e)
{
lblFullName.Text = PDM.varSession.getFullName
();
if (!Page.IsPostBack)
{
string strConn = ConfigurationSettings.AppS
ettings["C
ONNECTION_
STRING_PDM
"];
SqlConnection conDB = new SqlConnection(strConn);
SqlCommand myCMD;
myCMD = new SqlCommand("Select * From PDM_CODE_TYPES", conDB);
SqlDataAdapter adapter = new SqlDataAdapter(myCMD);
DataSet dataset = new DataSet();
adapter.Fill(dataset,"PDM_
CODE_TYPES
");
// apply sort to the DefaultView to sort by CompanyName
dataset.Tables[0].DefaultV
iew.Sort = "CODE_TYPE_NAME";
DropDownList1.DataSource = dataset.Tables["PDM_CODE_T
YPES"].Def
aultView;
DropDownList1.DataTextFiel
d = "CODE_TYPE_NAME"; // what to display
DropDownList1.DataValueFie
ld = "CODE_TYPE_ID"; // what to set as value
DropDownList1.DataBind();
BindData();
}
}
void BindData()
{
string strConn = ConfigurationSettings.AppS
ettings["C
ONNECTION_
STRING_PDM
"];
SqlConnection conn = new SqlConnection(strConn);
SqlDataAdapter Cmd = new SqlDataAdapter(sql, conn);
DataSet DS = new DataSet();
conn.Open();
Cmd.Fill(DS);
DataGrid1.DataSource = DS;
DataGrid1.DataBind();
conn.Close();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.CancelComma
nd += new System.Web.UI.WebControls.
DataGridCo
mmandEvent
Handler(th
is.DataGri
d1_CancelC
ommand);
this.DataGrid1.EditCommand
+= new System.Web.UI.WebControls.
DataGridCo
mmandEvent
Handler(th
is.DataGri
d1_EditCom
mand);
this.DataGrid1.ItemDataBou
nd += new System.Web.UI.WebControls.
DataGridIt
emEventHan
dler(this.
DataGrid1_
ItemDataBo
und);
this.DropDownList1.Selecte
dIndexChan
ged += new System.EventHandler(this.D
ropDownLis
t1_Selecte
dIndexChan
ged);
this.Button1.Click += new System.EventHandler(this.B
utton1_Cli
ck);
this.Load += new System.EventHandler(this.P
age_Load);
}
#endregion
private void DataGrid1_EditCommand(obje
ct source, System.Web.UI.WebControls.
DataGridCo
mmandEvent
Args e)
{
DataGrid1.EditItemIndex=e.
Item.ItemI
ndex;
BindData();
}
private void DataGrid1_ItemDataBound(ob
ject sender, System.Web.UI.WebControls.
DataGridIt
emEventArg
s e)
{
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingIt
em))
{
((Button)e.Item.FindContro
l("edit"))
.Visible = true;
((Button)e.Item.FindContro
l("update"
)).Visible
= false;
((Button)e.Item.FindContro
l("delete"
)).Visible
= false;
((Button)e.Item.FindContro
l("cancel"
)).Visible
= false;
string bgColor;
if (e.Item.ItemType == ListItemType.Item)
bgColor = "#EFF5DA";
else
bgColor = "#F0FFBB";
Label currencyName = (Label)e.Item.FindControl(
"currencyN
ame");
Label isoCode = (Label)e.Item.FindControl(
"iso");
currencyName.Text = Convert.ToString(DataBinde
r.Eval(e.I
tem.DataIt
em, "NAME"));
isoCode.Text = Convert.ToString(DataBinde
r.Eval(e.I
tem.DataIt
em, "CODE"));
e.Item.Attributes.Add("onm
ouseover",
"this.style.backgroundColo
r='#add8e6
'");
e.Item.Attributes.Add("onm
ouseout", "this.style.backgroundColo
r='" + bgColor + "'");
}
if (e.Item.ItemType == ListItemType.EditItem)
{
((Button)e.Item.FindContro
l("edit"))
.Visible = false;
((Button)e.Item.FindContro
l("update"
)).Visible
= true;
((Button)e.Item.FindContro
l("delete"
)).Visible
= true;
((Button)e.Item.FindContro
l("cancel"
)).Visible
= true;
TextBox txtName = (TextBox)e.Item.FindContro
l("txtName
");
TextBox txtISO = (TextBox)e.Item.FindContro
l("txtISO"
);
HtmlInputHidden dbCurrencyName = (HtmlInputHidden)e.Item.Fi
ndControl(
"dbCurrenc
yName");
Label currencyID = (Label)e.Item.FindControl(
"currencyI
D");
txtName.Text = Convert.ToString(DataBinde
r.Eval(e.I
tem.DataIt
em, "NAME"));
txtISO.Text = Convert.ToString(DataBinde
r.Eval(e.I
tem.DataIt
em, "code"));
dbCurrencyName.Value = Convert.ToString(DataBinde
r.Eval(e.I
tem.DataIt
em, "NAME"));
currencyID.Text = Convert.ToString(DataBinde
r.Eval(e.I
tem.DataIt
em, "ID"));
}
}
private void DataGrid1_CancelCommand(ob
ject source, System.Web.UI.WebControls.
DataGridCo
mmandEvent
Args e)
{
DataGrid1.EditItemIndex=-1
;
BindData();
}
private void DropDownList1_SelectedInde
xChanged(o
bject sender, System.EventArgs e)
{
}
private void Button1_Click(object sender, System.EventArgs e)
{
}
}
Any help would be appreciated.