i need to convert a field in gridview to a dropdownlist,
but i need to do this in codebehind, and I cannot add a templatefield in apsx(but it could be created at run time execution...)
I populate my grid with this code:
foreach (var item in response.Select(x => x.idMatriz).Distinct())
{
dr = dt.NewRow();
for (int i = 0; i < colunas; i++)
{
dr[i] = response.Where(x => x.Propriedade == dt.Columns[i].ToString() && x.idMatriz == item).Select(x => x.Valor).FirstOrDefault();
}
dt.Rows.Add(dr);
}
It works but i need this fileds be a dropdown....
any help?
It looks like all you need to do is dynamically create a template field and add it to the gridview.
var field = new TemplateField {HeaderText = col.ColumnName}
gridView.Columns.Add(field);
After that, on the row created event of the gridview create and wire up the dropdown.
public void DynamicGridView_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.DataRow)
{
return;
}
var grid = sender as GridView;
if (grid == null)
{
return;
}
for (var i = 0; i < grid.Columns.Count; i++)
{
var column = grid.Columns[i] as TemplateField;
if (column == null)
continue;
var cell = e.Row.Cells[i];
var dropdown = new DropDownList();
cell.Controls.Add(dropdown);
}
}
Related
How can I check all the records filtered by CheckAllBox event without using visible rows? (now it checks only the ones that visible on the screen).
this is the code behind:
private void CheckAllBox_OnClick(object sender, RoutedEventArgs e){
var grid = itemsGrid;
VisibleRows = new List<string>();
if (grid == null)
return;
//Getting the visible rows in dataGrid after filter
foreach (var item in grid.Items)
{
var row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(item);
if (row == null) continue;
if (row.TransformToVisual(grid).Transform(new Point(0, 0)).Y + row.ActualHeight >= grid.ActualHeight) break;
var item = (ItemParameter)row.DataContext;
VisibleRows.Add(qdf.Qdf);
}
var vm = (ItemsPaneViewModel)DataContext;
var checkBox = (CheckBox)sender;
if (checkBox.IsChecked == null) return;
var value = checkBox.IsChecked.Value;
foreach (var ItemParameter in vm.ItemParameters)
{
if (VisibleRows.Contains(ItemParameter.Name))
ItemParameter.IsEnable = value;
}
}
Thanks!
I have the following code to load data from a list in sharepoint website to a custom web part.
private void Data_load()
{
DataTable dt = new DataTable();
string currentName = SPContext.Current.Web.CurrentUser.Name;
SPQuery query = new SPQuery();
query.Query = "<Where><Eq><FieldRef Name='Editor'/><Value Type='Person or Group'>" + currentName + "</Value></Eq></Where>";
using (SPSite site = new SPSite("http://spdev-6/"))
{
using (SPWeb web = site.OpenWeb())
{
SPList lists = web.GetList("Lists/Advertisements");
SPListItemCollection items = lists.GetItems(query);
if (items.Count > 0)
{
//foreach(SPListItem item in items)
//{
// string decodeDescrip = Server.HtmlDecode( item["Details"].ToString());
// item["Details"] = decodeDescrip;
//}
dt = items.GetDataTable();
}
else
lbldata.Text = "No data to show";
GridViewD.DataSource = dt;
GridViewD.DataBind();
HttpContext.Current.Session["Advertisement"] = dt;
}
}
}
Now the issue is i am getting Htmlencode data in one of the columns. Now i have to remove it from grid view. So how it can be removed ?
You can modify the Column in the DataTable before assigning it to the Grid. Look here and here.
protected void GridView1_DataBound(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
for (int i = 0; i < row.Cells.Count - 1; i++)
{
if (row.Cells[i].Text == "0")
{
row.Cells[i].Text = "";
}
}
}
}
You could also change the value on DataBinding as described in this SO Answer.
protected void GridView1_DataBound(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
for (int i = 0; i < row.Cells.Count - 1; i++)
{
if (row.Cells[i].Text == "0")
{
row.Cells[i].Text = "";
}
}
}
}
i just want to bind two drop down list dynamically when grid view in row editing mode. here i declares one code block that dynamically fetches that row state and binds up those two drop down list.
here is code :
protected void GV_ViewCustomers_RowDataBound(object sender, GridViewRowEventArgs e)
{
...............
if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
{
using (DataClassesDataContext db = new DataClassesDataContext())
{
DropDownList dl = (DropDownList)e.Row.FindControl("DDL_Types1");
dl.DataSource = db.PartyTypes.Select(t => t).ToList();
dl.DataBind();
dl.SelectedValue = DataBinder.Eval(e.Row.DataItem, "type_id").ToString();
DropDownList dl1 = (DropDownList)e.Row.FindControl("DDL_CountryNames1");
dl1.DataSource = db.Countries.Select(c => c).ToList();
if (!string.IsNullOrEmpty(DataBinder.Eval(e.Row.DataItem, "country_id").ToString()))
{
dl1.SelectedValue = DataBinder.Eval(e.Row.DataItem, "country_id").ToString();
DropDownList dl2 = (DropDownList)e.Row.FindControl("DDL_StateNames1");
dl2.DataSource = db.States.Where(s => s.country_id.Equals(int.Parse(DataBinder.Eval(e.Row.DataItem, "country_id").ToString()))).Select(s => s).ToList();
dl2.DataBind();
}
DataRowView rowView1 = (DataRowView)e.Row.DataItem;
if (rowView1["UserOFC"] != null)
{
(e.Row.FindControl("chk_UserOFC1") as CheckBox).Checked = Convert.ToBoolean(e.Row.DataItem.Equals("UserOFC").ToString());
}
if (rowView1["UserVAT"] != null)
{
(e.Row.FindControl("chk_UserVAT1") as CheckBox).Checked = Convert.ToBoolean(e.Row.DataItem.Equals("UserVAT").ToString());
}
if (rowView1["UserINV"] != null)
{
(e.Row.FindControl("chk_UserINV1") as CheckBox).Checked = Convert.ToBoolean(e.Row.DataItem.Equals("UserINV").ToString());
}
if (rowView1["UserNone"] != null)
{
(e.Row.FindControl("chk_UserNone1") as CheckBox).Checked = Convert.ToBoolean(e.Row.DataItem.Equals("UserNone").ToString());
}
}
}
..................
}
Is this right method to binds drop down list at row editing mode.
please help me....
It's fine.But there are some other ways.
I have added gridview that is binds with csv file data that stored in session variable like this:
dgData.DataSource = Session["csvdata"];
dgData.DataBind();
It binds perfectly.But i need to add dropdownlist at first row of gridview for mapping of database column name with grid header.I used this code to add dropdownlist.But dropdown added to header instead of first row.
protected void dgData_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header )
{
for (Int32 i = 0; i < e.Row.Cells.Count; i++)
{
DropDownList ddl = new DropDownList();
ddl.ID = "ddlCol" + i.ToString ();
e.Row.Cells[i].Controls.Add(ddl);
}
}
}
protected void dgData_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl;
if (e.Row.RowIndex == 0)
{
for (Int32 i = 0; i < e.Row.Cells.Count; i++)
{
ddl = new DropDownList();
ddl.ID = "ddlCol" + i.ToString ();
e.Row.Cells[i].Controls.Add(ddl);
}
}
}
}
this code add's control to exatly first row of grid-view
I have a method called BindItems() and this method gets the values to a DataTable and then a GridView is bound to it.
Then I have an itemtemplate in that gridview which contains a drop down, this drop down gets populated from 1 -14 from code behind under Gridview_RowDataBound, now I need to figure a way to get the Quantity value in the DataTable on the other function "BindItems()" and for each Row in the gridview so a SelectedValue = datatable["Quantity"] or something.. how can I do this?
protected void BinItems(int myId)
{
//this data table contains a value "Quantity"
DataTable dt = MyClass.getItems(myId);
uxGrid.DataSource = dt;
uxGrid.DataBind();
}
protected void Gridview1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType.Equals(DataControlRowType.DataRow))
{
DropDownList Myddl = e.Row.FindControl("MyQuantity") as DropDownList;
if (Myddl != null)
{
for (int i = 1; i < 15; i++)
{
Myddl.Items.Add(i.ToString());
}
}
}
//Some how i need to do a SelectedValue = datatable where field = Quantity for each row
}
You need to access the current-being-bound-row's DataItem, which in your case, is a DataRowView (since you are binding to a DataTable):
protected void Gridview1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType.Equals(DataControlRowType.DataRow))
{
DropDownList Myddl = e.Row.FindControl("MyQuantity") as DropDownList;
if (Myddl != null)
{
for (int i = 1; i < 15; i++)
{
Myddl.Items.Add(i.ToString());
}
}
}
//here goes the "magic"
DataRowView dRow = e.Row.DataItem as DataRowView;
if(dRow != null)
{
Myddl.SelectedValue = dRow["Quantity"].ToString();
}
}
you should outside the DataTable dt and then youll be able to access it in the Function