I am currently developing a small program in C#.NET. Since I am just beginning to learn this language, I decided to work on DataGrid since it is a very good component used throughout the web.
Let's say I have a DataGrid (dgOne) and within dgOne I have 2 columns. I also have a button called btnShow.
First column contains DDL (ddlOne).
Second column contains TextBox (txtOne).
What I want to achieve is that upon the click of btnShow I will populate dgOne with values retrieved from the database.
I have tested the codes populating only a single DDL (not in a DataGrid) and it works fine. I just somehow can't get the program to populate the DDL. Anyone would be able to guide this newbie out? Any suggestions, advices, links will be greatly appreciated.
Loading

sanoj kumar39 reputationPosted Nov 14, 2006, 12:00 AM EST
((DropDownList)dg.Items[Row index].FindControl("ddlone")).DataSource=dataset;//dg is the datagrid
((DropDownList)dg.Items[Rowindex].FindControl("ddlone")).DataValueField="";
((DropDownList)dg.Items[Rowindex].FindControl("ddlone")).DataMember ="";
((DropDownList)dg.Items[Rowindex].FindControl("ddlone")).DataBind();
if you don't have dataset then
((DropDownList)dg.Items[index].FindControl("ddlone")).Items.Add(<
value>);Tell me if it helped you...
Best Career Wishes
Maruli Lee11 reputationPosted Nov 14, 2006, 8:42 PM EST
Hi Sanoj,
Thank you for your suggestions. I have implemented the code but my grid is still not showing anything. Let's say if I want to populate 5 DDL upon page load, can I use a for loop like this?
SWIFTDS.FS_FR_CHARGESMASTERDataTable pTable = null;
pTable = getCharges();
for(int i = 0, i < 5; i++)
{
((DropDownList)dgCharges.Items[i].FindControl("cboCharges")).DataSource=pTable;
((DropDownList)dgCharges.Items[i].FindControl("cboCharges")).DataValueField="CHARGEDESC";
((DropDownList)dgCharges.Items[i].FindControl("cboCharges")).DataMember ="CHARGEID";
((DropDownList)dgCharges.Items[i].FindControl("cboCharges")).DataBind();
}
FYI, I am putting a DDL called cboCharges into the first column of the dg (inside the ItemTemplate). I saw there is another column called "EditItem Template". Did I misplaced the DDL?
sanoj kumar39 reputationPosted Nov 15, 2006, 6:47 PM EST
look it(I only added 1 column).
the second column will come after
i'm using DataReader to bind DDL.
All the best.
Maruli Lee11 reputationPosted Nov 15, 2006, 8:54 PM EST
Hi Sanoj,
GridLines="Vertical" CellPadding="3" BackColor="White" BorderWidth="1px" BorderStyle="None"
My aspx code is as follow:
BorderColor="#999999" PageSize="5" AutoGenerateColumns="False" Height="304px" Width="544px"
AllowPaging="True">
The records that I retrieved from the database are stored inside a DataSet:
What I want to achieve is to populate the DDL upon clicking on a button (btnShow). Currently I did it like this:
private void btnShow_Click(object sender, System.EventArgs e)
{
try
{
SWIFTDS.FS_FR_CHARGESMASTERDataTable pTable = null;
pTable = getCharges(); //Retrieve data from db
pTable.DefaultView.Sort = "CHARGEID";
int i = 0;
foreach (DataGridItem di in dgCharges.Items)
{
if (di.ItemType == ListItemType.Item || di.ItemType == ListItemType.AlternatingItem)
{
((DropDownList)dgCharges.Items[i].FindControl("cboCharges")).DataSource=pTable;
((DropDownList)dgCharges.Items[i].FindControl("cboCharges")).DataValueField="CHARGEDESC";
((DropDownList)dgCharges.Items[i].FindControl("cboCharges")).DataTextField="CHARGEDESC";
((DropDownList)dgCharges.Items[i].FindControl("cboCharges")).DataMember ="CHARGEID";
((DropDownList)dgCharges.Items[i].FindControl("cboCharges")).DataBind();
}
i++;
}
}
catch(System.Exception er)
{
// Do something here
}
}
Despite doing this, I still did not see anything when I clicked on the Show button. I didn't even see the datagrid at all. Is there anything that I missed out?
sanoj kumar39 reputationPosted Nov 16, 2006, 1:19 AM EST
I think i got your problem.
Do you have any dataset to bind the datagrid?
To view your datagrid you must bind it.because
the datagrid have no values to display.so not load.
In your case you can do it before the for each loop.
And note that if there is no rows in datagrid that
foreach loop will never execute.
If you haven't the need to bind grid You can use datatable.
If nee more help reply.
Maruli Lee11 reputationPosted Nov 19, 2006, 9:56 PM EST
Hey again Sanoj,
I have managed to populate the DDLs as well as show the datagrid upon page load.
What I did was:
1. Upon page load, I bind the datagrid
2. Populate the DDL inside the datagrid_ItemDataBound event
I have another question please. I do not want the grid to show up (and the DDL to populate themselves) upon page load. I only want to do that when user click on a button because I would like to filter the data based on a UniqueID that user will be entering into a textbox.
Any ideas/suggestions?
sanoj kumar39 reputationPosted Nov 21, 2006, 6:38 PM EST
I don't know what you meant, if you want to display the grid when button pressed then
don't bind the grid at page load do it in the button pressed event.
All the best...
Maruli Lee11 reputationPosted Nov 22, 2006, 8:34 PM EST
private
void btnShow_Click(object sender, System.EventArgs e){
pTable = getCharges();
if (pTable != null)
{
pTable.DefaultView.Sort = "CHARGEID";
DataView pDV = pTable.DefaultView;
dgCharges.DataSource = pDV;
dgCharges.DataBind();
}
sanoj kumar39 reputationPosted Nov 25, 2006, 12:24 AM EST
Are you sure the dataview have data?
I am using Datatable for purposes like this.
I think it can done using datatable.
Fill datas to datatable and give it as datasource,it will work.
Maruli Lee11 reputationPosted Nov 26, 2006, 8:19 PM EST
Hi Sanoj,
Could you please provide me with a snippets on how to work with DataTable?
Thank you for your help time and time again.
nilesh rathod8 reputationPosted Nov 29, 2006, 2:15 AM EST
nilesh rathod8 reputationPosted Nov 29, 2006, 2:16 AM EST