Asp.net - DropDownLists in GridView - asp.net

I was trying to make a column of DropDownLists in GridView from the C# code, to no avail. Here is the code I use:
var manager=Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add("Email", typeof(String));
dt.Columns.Add("Username", typeof(String));
dt.Columns.Add("Roles", typeof(DropDownList));
var a = manager.Users.ToArray();
for (int i = 0; i < a.Length; i++ )
{
dr = dt.NewRow();
dr["Email"] = a[i].Email;
dr["Username"] = a[i].UserName;
DropDownList lista = new DropDownList();
var b = manager.Users.ToArray();
lista.DataSource = b;
lista.DataBind();
dr["Roles"] = (DropDownList)lista;
dt.Rows.Add(dr);
}
GridView1.AutoGenerateSelectButton = true;
dt.AcceptChanges();
GridView1.DataSource = dt;
GridView1.DataBind();
The first 2 colums show up, the last (which should have DropDownLists) does not. Why?

Related

Display Same data in row and column in asp.net gridview from codebehind?

enter image description hereDisplay Same data in row and column in asp.net gridview from codebehind ?
It is not clear from your question but try this.
//Create your Original DataSource
DataTable dt = new DataTable();
DataColumn dc1 = new DataColumn("col1");
DataColumn dc2 = new DataColumn("col2");
DataColumn dc3 = new DataColumn("col3");
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
dt.Columns.Add(dc3);
DataRow dr = dt.NewRow();
dr[0] = "A";
dr[01] = "B";
dr[2] = "C";
dt.Rows.Add(dr);
//Adding Rows
for (int i = 1; i < dt.Columns.Count; i++)
{
DataRow dr1 = dt.NewRow();
dr1[0] = dt.Rows[0][i].ToString();
dt.Rows.Add(dr1);
}
GridViewName.DataSource = dt;
GridViewName.DataBind();

Counter doesn't increment

Relatively new to coding, so there might be a clear mistake in the code. What I am trying to do is have a counter that increases by one each time a new product is added to the basket.
When the program is run, its default value is 0, when the you click on the first product to add to the basket, it increases to 1, however, if you add any more products it sticks at 1 instead of increasing each time.
I have included the code below - is there an obvious mistake I am over looking being a novice (I'll be the first to attempt that ha!). No error message is thrown at any point.
protected void BtnAddToBasket_Click(object sender, EventArgs e)
{
string ProductID = Convert.ToInt16((((Button)sender).CommandArgument)).ToString();
string ProductStock = "1";
DataListItem currentItem = (sender as Button).NamingContainer as DataListItem;
Label lblAvailableStock = currentItem.FindControl("lblAvailableStock") as Label;
if (Session["MyBasket"] != null)
{
DataTable dt = (DataTable)Session["My Basket"];
var checkProduct = dt.AsEnumerable().Where(r => r.Field<string>("ProductID") == ProductID);
if (checkProduct.Count() == 0)
{
string query = "SELECT * FROM Product WHERE ProductID = " + ProductID + "";
DataTable dtProduct = GetData(query);
DataRow dr = dt.NewRow();
dr["ProductID"] = ProductID;
dr["ProductName"] = Convert.ToString(dtProduct.Rows[0]["ProductName"]);
dr["ProductDescription"] = Convert.ToString(dtProduct.Rows[0]["ProductDescription"]);
dr["ProductPrice"] = Convert.ToString(dtProduct.Rows[0]["ProductPrice"]);
dr["ProductStock"] = ProductStock;
dr["ProductImageUrl"] = Convert.ToString(dtProduct.Rows[0]["ProductImageUrl"]);
dr["AvailableStock"] = lblAvailableStock;
dt.Rows.Add(dr);
Session["MyBasket"] = dt;
btnShoppingBasket.Text = dt.Rows.Count.ToString();
}
}
else
{
string query = "SELECT * FROM Product WHERE ProductID =" + ProductID + "";
DataTable dtProduct = GetData(query);
DataTable dt = new DataTable();
dt.Columns.Add("ProductID", typeof(string));
dt.Columns.Add("ProductName", typeof(string));
dt.Columns.Add("ProductDescription", typeof(string));
dt.Columns.Add("ProductPrice", typeof(string));
dt.Columns.Add("ProductStock", typeof(string));
dt.Columns.Add("ProductImageUrl", typeof(string));
dt.Columns.Add("AvailableStock", typeof(string));
DataRow dr = dt.NewRow();
dr["ProductID"] = ProductID;
dr["ProductName"] = Convert.ToString(dtProduct.Rows[0]["ProductName"]);
dr["ProductDescription"] = Convert.ToString(dtProduct.Rows[0]["ProductDescription"]);
dr["ProductPrice"] = Convert.ToString(dtProduct.Rows[0]["ProductPrice"]);
dr["ProductStock"] = ProductStock;
dr["ProductImageUrl"] = Convert.ToString(dtProduct.Rows[0]["ProductImageUrl"]);
dr["AvailableStock"] = lblAvailableStock;
dt.Rows.Add(dr);
Session["My Basket"] = dt;
btnShoppingBasket.Text = dt.Rows.Count.ToString();
}
}
Counter
From what I see it appears that you create new basket every time an item is added into the basket. The little mistake is that you use different names of saved basket entry in session:
if (Session["MyBasket"] != null)
This will be false first time so it goes into else block.
Session["My Basket"] = dt;
Here you store new DataTable with different key. So the next time the if statement will still be false. So just make sure that the name is consistent. "MyBasket" != "My Basket"
Fixed where "MyBasket" was "My Basket"; reduced code duplication. The misspelling is a compelling reason to use constants instead of string literals.
protected void BtnAddToBasket_Click(object sender, EventArgs e)
{
string ProductID = Convert.ToInt16((((Button)sender).CommandArgument)).ToString();
string ProductStock = "1";
DataListItem currentItem = (sender as Button).NamingContainer as DataListItem;
Label lblAvailableStock = currentItem.FindControl("lblAvailableStock") as Label;
if (Session["MyBasket"] == null)
{
DataTable dt = new DataTable();
dt.Columns.Add("ProductID", typeof(string));
dt.Columns.Add("ProductName", typeof(string));
dt.Columns.Add("ProductDescription", typeof(string));
dt.Columns.Add("ProductPrice", typeof(string));
dt.Columns.Add("ProductStock", typeof(string));
dt.Columns.Add("ProductImageUrl", typeof(string));
dt.Columns.Add("AvailableStock", typeof(string));
}
DataTable dt = (DataTable)Session["MyBasket"];
var checkProduct = dt.AsEnumerable().Where(r => r.Field<string>("ProductID") == ProductID);
if (checkProduct.Count() == 0)
{
string query = "SELECT * FROM Product WHERE ProductID = " + ProductID + "";
DataTable dtProduct = GetData(query);
DataRow dr = dt.NewRow();
dr["ProductID"] = ProductID;
dr["ProductName"] = Convert.ToString(dtProduct.Rows[0]["ProductName"]);
dr["ProductDescription"] = Convert.ToString(dtProduct.Rows[0]["ProductDescription"]);
dr["ProductPrice"] = Convert.ToString(dtProduct.Rows[0]["ProductPrice"]);
dr["ProductStock"] = ProductStock;
dr["ProductImageUrl"] = Convert.ToString(dtProduct.Rows[0]["ProductImageUrl"]);
dr["AvailableStock"] = lblAvailableStock;
dt.Rows.Add(dr);
Session["MyBasket"] = dt;
btnShoppingBasket.Text = dt.Rows.Count.ToString();
}
}

Placing DataBound Drop Down List into TableCell

I have a bound drop down list that I would like to place into a Table Cell. I am used to Labels and Text Boxes but I cannot seem to get the syntax working on this one. This code is inside of a Webmethod and my output has to be just the html. Thank you.
[System.Web.Services.WebMethod]
public static string gatherSurchargeData(string PriceListItemID, string ProdID, string ColorCode)
{
DataTable GetProductSizes = new DataTable();
GetProductSizes = DataLayer.PricingToolDL.getProductSizes(ProdID, ColorCode);
DataTable dt = new DataTable();
dt = DataLayer.PricingToolDL.getScharge(PriceListItemID);
Table tblScharges = new Table();
tblScharges.ID = "tblScharges";
TableHeaderRow th = new TableHeaderRow();
TableHeaderCell thSizeScharge = new TableHeaderCell();
thSizeScharge.Text = "Size";
th.Cells.Add(thSizeScharge);
tblScharges.Rows.Add(th);
int i = 0;
while (i <= dt.Rows.Count - 1)
{
TableRow tr = new TableRow();
tr.ID = "tableTr" + i;
TableCell tcSizeScharge = new TableCell();
DropDownList ddl = new DropDownList();
ddl.DataSource = GetProductSizes;
ddl.DataTextField = "FitSize";
ddl.DataValueField = "FitSize";
ddl.DataBind();
//string dtMovexSKU = dt.Rows[i]["MovexSKU"].ToString();
//DataRow[] GetProductSizesMovexSKU = GetProductSizes.Select("MovexSKU Like'" + dtMovexSKU + "'");
//tcSizeScharge.ID = "tcSizeScharge" + i;
//tcSizeScharge.Text = GetProductSizesMovexSKU[0][1].ToString();
tr.Cells.Add(tcSizeScharge);
tblScharges.Rows.Add(tr);
i++;
}
string html = "";
using (StringWriter sw = new StringWriter())
{
tblScharges.RenderControl(new HtmlTextWriter(sw));
html = sw.ToString();
}
return html;
}
The commented lines would be the code I would use if I were only wanting text to appear if that helps.
You need to add control in tablecell like this
tcSizeScharge.Controls.Add(ddl);

Filling data in grid view using textbox on button click -it show an a error inder out of bound

when i used a textbox to insert data it show index out of bound error
Button_Click()
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add("Name"); ///enter code here
dt.Columns.Add("Address");
dt.Columns.Add("Number");
//First fill all the date present in the grid
for (int intCnt = 0; intCnt < grd.Rows.Count; intCnt ++)
{
if (grd.Rows[intCnt].RowType == DataControlRowType.DataRow)
{
dr = dt.NewRow();
dr["Name"] = grd.Rows[intCnt].Cells[0].Value;/// at this point
dr["Address"] = grd.Rows[intCnt].Cells[1].Value;
dr["Number"] = grd.Rows[intCnt].Cells[2].Value;
dt.Rows.Add(dr);
}
}
dr = dt.NewRow();
dr["Name"] = txt1.Text;
dr["Address"] = txt2.Text;
dr["Number"] = txt3.Text;
dt.Rows.Add(dr);
grd.DataSource = dt;
grd.DataBind();
}
Try this
You have to use grd.Rows.Count-1 in for loop
Button_Click()
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add("Name");
dt.Columns.Add("Address");
dt.Columns.Add("Number");
for (int intCnt = 0; intCnt < grd.Rows.Count-1; intCnt ++)
{
if (grd.Rows[intCnt].RowType == DataControlRowType.DataRow)
{
dr = dt.NewRow();
dr["Name"] = grd.Rows[intCnt].Cells[0].Value;/// at this point
dr["Address"] = grd.Rows[intCnt].Cells[1].Value;
dr["Number"] = grd.Rows[intCnt].Cells[2].Value;
dt.Rows.Add(dr);
}
}
dr = dt.NewRow();
dr["Name"] = txt1.Text;
dr["Address"] = txt2.Text;
dr["Number"] = txt3.Text;
dt.Rows.Add(dr);
grd.DataSource = dt;
grd.DataBind();
}
Hope this help you
change the condition of your for loop to be for (int intCnt = 0; intCnt < grd.Rows.Count-1; intCnt ++)
{...}
instead of for (int intCnt = 0; intCnt < grd.Rows.Count; intCnt ++)
{...}

Controls in DataGrid WPF with dynamic columns

I need to put a stackpanel inside the table cell but if i did my logic like this....
DataGrid dg = new DataGrid();
StackPanel sp = new StackPanel();
sp.Height = 18;
sp.Width = 60;
sp.Orientation = Orientation.Horizontal;
Button btn = new Button();
btn.Width = 10;
btn.Height = 10;
sp.Children.Add(btn);
Label bt = new Label();
bt.Content = "test";
bt.Margin = new Thickness(0, -3, 0, 0);
sp.Children.Add(bt);
DataTable dt = new DataTable();
DataColumn dc = new DataColumn("hello");
dt.Columns.Add(dc);
DataRow dr = dt.NewRow();
dr[0] = sp; dt.Rows.Add(dr);
dg.ItemsSource = dt.DefaultView;
l1.Content = dg;
it is showing "System.Windows.Controls.StackPanel" in the cell..... how to get a StackPanel(with multiple Controls) in side the GridView cell
You should create custom template for your grid cells. See this.

Resources