In my asp page when press a button, a gridview will be populated with data from db, but beside the columns from database i need a new dynamic column in gridview, to contain only dropdownlist with items number from 0-n.
This far i managed to do it, with some research on internet (i used an intemerdiet datatable to apply ItemTemplate from GridViewTemplat e class, and than merged it to datatable containg the result from query, so the gridview's datasource si the merge of this 2 datatables).
The issue is that i don't now how can i access the values selected by user in the dinamically dropdownlists, 'cause i have to save them in DB.
Attach is my GridViewTemplat e class code, and .cs and .aspx code.
GridViewTemplat e class
.cs code
.aspx code
Thank you in advance.
This far i managed to do it, with some research on internet (i used an intemerdiet datatable to apply ItemTemplate from GridViewTemplat e class, and than merged it to datatable containg the result from query, so the gridview's datasource si the merge of this 2 datatables).
The issue is that i don't now how can i access the values selected by user in the dinamically dropdownlists, 'cause i have to save them in DB.
Attach is my GridViewTemplat e class code, and .cs and .aspx code.
GridViewTemplat e class
Code:
public class GridViewTemplate : ITemplate
{
//A variable to hold the type of ListItemType.
ListItemType _templateType;
//A variable to hold the column name.
string _columnName;
//Constructor where we define the template type and column name.
public GridViewTemplate(ListItemType type, string colname)
{
//Stores the template type.
_templateType = type;
//Stores the column name.
_columnName = colname;
}
void ITemplate.InstantiateIn(System.Web.UI.Control container)
{
switch (_templateType)
{
case ListItemType.Header:
//Creates a new label control and add it to the container.
Label lbl = new Label(); //Allocates the new label object.
lbl.Text = _columnName; //Assigns the name of the column in the lable.
container.Controls.Add(lbl); //Adds the newly created label control to the container.
break;
case ListItemType.Item:
//Creates a new dro down list control and add it to the container.
DropDownList ddl = new DropDownList(); //Allocates the new text box object.
ddl.DataBinding += new EventHandler(ddl_DataBinding); //Attaches the data binding event.
ddl.Width = 35; //Creates a column with size 4.
ddl.Items.Add("0"); //Fill in the dropdownlist with values from 0-4
ddl.Items.Add("1");
ddl.Items.Add("2");
ddl.Items.Add("3");
ddl.Items.Add("4");
//ddl.ID = "dropdownlist1"; // the ddl name will end with dropdownlist1, all of them, can use findcontrol method...but..
container.Controls.Add(ddl); //Adds the newly created textbox to the container.
break;
case ListItemType.EditItem:
//As, I am not using any EditItem, I didnot added any code here.
break;
case ListItemType.Footer:
CheckBox chkColumn = new CheckBox();
chkColumn.ID = "Chk" + _columnName;
container.Controls.Add(chkColumn);
break;
}
}
void ddl_DataBinding(object sender, EventArgs e)
{
DropDownList ddd = (DropDownList)sender;
GridViewRow container = (GridViewRow)ddd.NamingContainer;
object dataValue = DataBinder.Eval(container.DataItem, _columnName);
if (dataValue != DBNull.Value)
{
ddd.Text = dataValue.ToString();
}
}
}
.cs code
Code:
public void btn_Matrix_Click(object sender, EventArgs e)
{
SqlConnection conn = null;
SqlDataAdapter myDataAdapter = null;
grdMatrix.Visible = true;
try
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
conn = new SqlConnection(connStr);
DataSet myDataSet = new DataSet();
myDataAdapter = new SqlDataAdapter("", conn);
myDataAdapter.SelectCommand.CommandText = "select employee_id as 'Employee No',lastname as 'Employee Name' from employee";
myDataAdapter.Fill(myDataSet, "Temp");
DataTable dtquery = myDataSet.Tables[0];
DataTable dtcolumns = new DataTable();
dtcolumns.Columns.Add("Level");
foreach (DataColumn col in dtcolumns.Columns)
{
//Declare the bound field and allocate memory for the bound field.
TemplateField bfield = new TemplateField();
//Initalize the DataField value.
bfield.HeaderTemplate = new GridViewTemplate(ListItemType.Header, col.ColumnName);
//Initialize the HeaderText field value.
bfield.ItemTemplate = new GridViewTemplate(ListItemType.Item, col.ColumnName);
//Add the newly created bound field to the GridView.
grdMatrix.Columns.Add(bfield);
}
dtcolumns.Merge(dtquery);
//Initialize the DataSource
grdMatrix.DataSource = dtcolumns;
//Bind the datatable with the GridView.
grdMatrix.DataBind();
conn.Close();
}
catch (Exception ex)
{
WebMsgBox.Show(ex.Message);
}
finally
{
if (conn != null)
conn.Close();
}
}
.aspx code
Code:
<br/><asp:Panel ID="Panel1" runat="server">
<asp:GridView ID="grdMatrix" runat="server" AutoGenerateColumns = "False"
CellPadding="4" EnableModelValidation="True" ForeColor="#333333"
GridLines="None" >
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="Employee No" Visible="true" HeaderText="Employee No" />
<asp:BoundField DataField="Employee Name" Visible="true" HeaderText="Employee Name" />
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
</asp:Panel>
Comment