I have GridView that is filled programically using List of Object. I can't access to Columns in GridView. I like to put Width of Columns programically?Here is the code how I fill the GridView
DataClassesDataContext dContext = new DataClassesDataContext();
var userId = (Guid)(Membership.GetUser(Membership.GetUser().UserName, false).ProviderUserKey);
var tekovenKorisnikQuery = from d in dContext.pregledIshranas
where d.UserId == userId
select new {d.datum, d.kilogrami, d.visina, d.BMI, d.kalorii};
List<PregledIshranaFormatirano> listaFormatirana = new List<PregledIshranaFormatirano>();
foreach (var d in tekovenKorisnikQuery)
{
listaFormatirana.Add(new PregledIshranaFormatirano(string.Format("{0:dd-MM-yyyy}", d.datum), d.kilogrami.ToString(), d.visina.ToString(), string.Format("{0:N2}", d.BMI), d.kalorii.ToString()));
}
gvTekovenKorisnik.DataSource = listaFormatirana;
gvTekovenKorisnik.DataBind();
I use this event handler to change header
protected void gvTekovenKorisnik_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].Width = new Unit("200px");
e.Row.Cells[0].Text = "Датум";
e.Row.Cells[1].Width = new Unit("200px");
e.Row.Cells[1].Text = "Килограми";
e.Row.Cells[2].Width = new Unit("200px");
e.Row.Cells[2].Text = "Висина";
e.Row.Cells[3].Width = new Unit("200px");
e.Row.Cells[3].Text = "BMI индекс";
e.Row.Cells[4].Width = new Unit("200px");
e.Row.Cells[4].Text = "Калории";
}
In this way the width isn't changed and I can't access to gridView columns.
Could somebody help me?
You can change column widths right after GridView DataBind() method call.
For example: gvTekovenKorisnik.Columns[0].Width = %yourvalue%;
Related
What im trying to do is to populate an empty Table with cells and the number of cells per row is 4 and the number of all cells is the number of nodes in btnNode
The empty table ID = "MainTable"
and so far i created this but it seems i get an error in
TableRowCollection rows = new TableRowCollection();
has no constructors defined.Searched in google and didnt find help to solve this issue
Here is my code :
private void linkBTN_Click(object sender, EventArgs e)
{
LinkButton btn = (LinkButton)sender;
string text = btn.Text.ToString();
XmlDocument clickDoc = new XmlDocument();
clickDoc.Load(Server.MapPath("~/ProductShow.xml"));
XmlNodeList btnNode = clickDoc.SelectNodes("products/" + text.ToString() + "/*");
int count = btnNode.Count;
TableRowCollection rows = new TableRowCollection();
if (count < 4)
{
TableRow row = new TableRow();
rows.Add(row);
}
else
{
for (int i = 0; i < count / 4; i++)
{
TableRow row = new TableRow();
rows.Add(row);
}
}
int j = 1;
foreach (XmlNode node in btnNode)
{
//TableRow row = new TableRow();
TableCell cell = new TableCell();
cell.CssClass = "Cell";
LinkButton linkbtn = new LinkButton();
linkbtn.Text = node.InnerText;
linkbtn.Attributes.Add("runat", "server");
Image img = new Image();
cell.Controls.Add(linkbtn);
cell.Controls.Add(new LiteralControl("<br/>"));
cell.Controls.Add(img);
rows[j].Cells.Add(cell);
j++;
}
foreach (TableRow row in rows)
{
MainTable.Rows.Add(row);
}
}
How to count the no of records in Gridview which have some particular data in a column
name result
======== ======
krishna pass
sanjay pass
ajay fail
out put needed in grid view - above Gridview already present,according to that grid i have to make another grid to count results
result no
====== =====
pass 2
fail 1
in data row bound , i calculated
protected void GVKeywordReport_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRow pr = ((DataRowView)e.Row.DataItem).Row;
int oldPos = Convert.ToInt32(pr["oldposition"]);
int newPos = Convert.ToInt32(pr["newposition"]);
GVKeywordReport.HeaderRow.Cells[3].Text = txtfrmdate.Text;
GVKeywordReport.HeaderRow.Cells[4].Text = txtEndDate.Text;
GVKeywordReport.HeaderRow.BackColor = ColorTranslator.FromHtml("#B3B300");
e.Row.Cells[0].BackColor = ColorTranslator.FromHtml("#B3B300");
e.Row.Cells[5].BackColor = ColorTranslator.FromHtml("#FFFFFF");
if (oldPos == newPos)
{
e.Row.BackColor = ColorTranslator.FromHtml("#FF950E");
e.Row.Cells[6].Text = "No Change";
nc= nc+1;
}
else if (oldPos > newPos)
{
e.Row.BackColor = ColorTranslator.FromHtml("#FFFFCC");
e.Row.Cells[6].Text = "Improved";
imprv= imprv+1;
}
else
{
e.Row.BackColor = ColorTranslator.FromHtml("#FF0000");
e.Row.Cells[6].Text = "Decreased";
decrs=decrs+1;
}
// e.Row.Cells[0].BackColor = ColorTranslator.FromHtml("#7DA647");
}
txt_TargetReached.Text = "0";
txtDecreased.Text =Convert.ToString(decrs);
In following code i am trying to show the latest record on the top of repeater. I also want to include paging in repeater.Paging is done successful in page but i am having trouble when i do sorting in repeater.So my question is this that how can i do sorting and paging in repeater?
My code:
private void Get_Data()
{
String File = Server.MapPath("~/Data/BlogContent.xml");
DataSet ds = new DataSet();
ds.ReadXml(File);
DataView dv = new DataView(ds.Tables[0]);
dv.Sort = "id DESC";
DataTable dt = dv.Table;
ViewState.Add("Mytable", dt);
}
private void Bind_Data(int take, int pageSize)
{
PagedDataSource page = new PagedDataSource();
page.AllowCustomPaging = true;
page.AllowPaging = true;
DataTable dtv = (DataTable)ViewState["Mytable"];
DataView dv = new DataView();
dv = dtv.DefaultView;
dv.Sort = "id ASC";
dv.RowFilter = "id>=" + pageSize + " AND " + "id<=" + take;
page.DataSource = dv;
page.PageSize = psize;
Repeater1.DataSource = page;
Repeater1.DataBind();
if (!IsPostBack)
{
int rowcount = dtv.Rows.Count;
CreatePagingControl(rowcount);
}
}
For paging Check this.Here is full example of paging.link
paging and sorting
** Read the article:
Custom paging in Repeater control in asp.net(C#, VB)**
http://www.webcodeexpert.com/2013/05/custom-paging-in-repeater-control-in.html
I have created a function which displays a bus seat layout, dynamically. Now after adding all this dynamically created table and cells into a tag, I want to know which imagebutton was clicked.I have also mentioned proper ID to each imagebutton in cell.
public void DisplaySeatLayout(ListBus _listBus) {
//fetching data from database
SeatBUS _seatBUS = new SeatBUS();
DataTable dt = _seatBUS.GetAllSeatByBusRouter(_listBus);
//Layout generation code
ImageButton img ;
HtmlTable table = new HtmlTable();
table.Attributes.Add("runat", "server");
table.Attributes.Add("id", "LayoutTable");
HtmlTableRow [] tr = new HtmlTableRow[] { new HtmlTableRow(), new HtmlTableRow(), new HtmlTableRow(),new HtmlTableRow(),new HtmlTableRow()};
HtmlTableCell tc = null;
int SeatNo=0;
//Getting Total no of seats.
int MaxSeatNo = dt.Rows.Count;
//Iterating datatable
//Displaying labels for displaying column names in the table
for (int columnCounter = 0; columnCounter < 8; columnCounter++){
for (int rowCounter = 0; rowCounter < 5; rowCounter++){
if (SeatNo < MaxSeatNo){
if (rowCounter == 2 && columnCounter < 7){
tc = new HtmlTableCell();
Label lbl = new Label();
lbl.Text = "";
lbl.ID = "lbl " + rowCounter.ToString() + columnCounter.ToString();
tc.Controls.Add(lbl);
tr[rowCounter].Controls.Add(tc);
//reducing seat number for sake of sequence.
}
else{
//adding label in each cell.
tc = new HtmlTableCell();
Label lbl = new Label();
lbl.Text = dt.Rows[SeatNo]["NumberSeat"].ToString();
lbl.ID = "lbl " + rowCounter.ToString() + columnCounter.ToString();
tc.Controls.Add(lbl);
//adding imagebutton in each cell .
img = new ImageButton();
img.Attributes.Add("type", "image");
img.Attributes.Add("id", rowCounter.ToString());
img.CssClass = "seatRightMostRow1";
img.ImageUrl = "../Images/available_seat_img.png";
img.ID = dt.Rows[SeatNo]["NumberSeat"].ToString();
img.Click += new ImageClickEventHandler(Imagebutton_Click);
tc.Controls.Add(img);
tr[rowCounter].Controls.Add(tc);
SeatNo++;
}
}//SeatNo < MaxSeatNo
table.Controls.Add(tr[rowCounter]);
}
seatArranngement.Controls.Add(table);
}
}
this is complete code after suggestion.
function that is displaying dynamic table is inside Page_load
protected void Page_Load(object sender, EventArgs e)
{
_listBusBUS = new ListBusBUS();
_routerBUS = new RouterBUS();
_listBus = new ListBus();
_promoteBUS = new PromoteBUS();
if (!Page.IsPostBack)
{
LoadData();
}
DisplaySeatLayout();
}
Also here i have copied code in prerender
protected override void OnPreRender(EventArgs e)
{
if (MultiView1.ActiveViewIndex == 1)
{
int listBusID = int.Parse(hdlListBusID.Value.ToString());
_listBus = _listBusBUS.GetAllListBusById(listBusID);
litListBusID.Text = _listBus.ListBusID.ToString();
litRouterID.Text = _listBus.RouterID.ToString();
litArrival.Text = _listBus.Arrival.ToString();
litDeparture.Text = _listBus.Departure.ToString();
litPrice.Text = _listBus.Price.ToString();
}
else if (MultiView1.ActiveViewIndex == 2)
{
//original code starts from here
litListBusIDS.Text = litListBusIDS.Text;
litRouterIDS.Text = litRouterID.Text;
litArrivalS.Text = litArrival.Text;
litDepartureS.Text = litDeparture.Text;
litSeat.Text = hdlSeat.Value;// chkSeat.SelectedItem.Text;
litPrices.Text = litPrice.Text;
//hdlSeat.Value = chkSeat.SelectedItem.Text.ToString();
}
else if (MultiView1.ActiveViewIndex == 3)
{
litCustomerNameStep4.Text = txtcustomername.Text;
litSeatStep4.Text = litSeat.Text;
litPriceStep4.Text = litPrices.Text;
}
base.PreRender(e);
}
Also, layout is getting created twice if i click on any imagebutton.
All you have to do is get the value that you already placed inside the "id" attribute.
You can do this inside the Imagebutton_Click even you created:
protected void Imagebutton_Click(object sender, EventArgs e)
{
ImageButton b = sender as ImageButton;
string id = b.Attributes["id"]; // Returns the id
}
I have an ObjectDataSource with an ID of ObjectDataSource1 on a webpage. I also have a gridview in which I am binding the ObjectDataSource.ID to the GridView.DataSourceID. The problem I get is when text is changed in a textbox, the code calls BrokerageTransactions.GetAllWithDt which returns a DataTable. I want to set this datatable as the DataSource for the GridView, but it is telling me that I can't set the DataSouce and DataSourceId together. How can I fix this? Code is below. Also. Why can't you set a DataSourceID and a DataSource when using an ObjectDataSource?
Thanks,
X
protected void BrokerageChange(Object sender, EventArgs e)
{
BrokerageTransactions brokerageaccountdetails =
new BrokerageTransactions();
DataSet ds = BrokerageAccount.GetBrkID2(new
Guid(Membership.GetUser().ProviderUserKey.ToString()),
ddlBrokerageDetails.SelectedItem.Text.ToString());
foreach (DataRow dr in ds.Tables[0].Rows)
{
brokerageaccountdetails.BrokerageId =
new Guid(dr["BrkrgId"].ToString());
}
ddlBrokerageDetails.SelectedItem.Value =
brokerageaccountdetails.BrokerageId.ToString();
if (txtTransactionsTo.Text != ""
&& txtTransactionsFrom.Text != "")
ObjectDataSource1.FilterExpression =
"convert(CreateDt,System.DateTime)>Convert('" +
Convert.ToDateTime(txtTransactionsFrom.Text) + "',System.DateTime)
and Convert(CreateDt,System.DateTime)<convert('"
+ Convert.ToDateTime(txtTransactionsTo.Text.ToString()) +
"',System.DateTime)";
else if (txtTransactionsFrom.Text != "")
ObjectDataSource1.FilterExpression =
"convert(CreateDt,System.DateTime)>convert('" +
Convert.ToDateTime(txtTransactionsFrom.Text) +
"',System.DateTime)";
else if (txtTransactionsTo.Text != "")
ObjectDataSource1.FilterExpression =
"convert(CreateDt,System.DateTime)
<convert('"
+ Convert.ToDateTime(txtTransactionsTo.Text.ToString()) +
"',System.DateTime)";
else
ObjectDataSource1.FilterExpression = " ";
grvBrokerage.DataSourceID = ObjectDataSource1.ID;
grvBrokerage.DataBind();
DateTime dtTransFrom = Convert.ToDateTime("1/1/1900");
DateTime dtTransTo = System.DateTime.Today;
//TransactionsTo Box is Empty
if ((txtTransactionsFrom.Text.Length > 2)
&& (txtTransactionsTo.Text.Length < 2))
{
dtTransFrom = Convert.ToDateTime(txtTransactionsFrom.Text);
dtTransTo = System.DateTime.Today;
}
//TransactionsFrom Box is Empty
if ((txtTransactionsFrom.Text.Length < 2)
&& (txtTransactionsTo.Text.Length > 2))
{
dtTransFrom = Convert.ToDateTime("1/1/1900");
dtTransTo = Convert.ToDateTime(txtTransactionsTo.Text);
}
//TransactionsFrom Box and TransactionsTo Box is Not Empty
if ((txtTransactionsFrom.Text.Length > 2)
&& (txtTransactionsTo.Text.Length > 2))
{
dtTransFrom = Convert.ToDateTime(txtTransactionsFrom.Text);
dtTransTo = Convert.ToDateTime(txtTransactionsTo.Text);
}
// Fails Here
grvBrokerage.DataSource =
BrokerageTransactions.GetAllWithDt(brokerageaccountdetails.BrokerageId,
dtTransFrom,
dtTransTo);
grvBrokerage.DataBind(); }
You got 2 options:
Do not use the ObjectDataSource at
all. You just use the DataSource
property and Set It grammatically
every time.
You use the DataSourceID property of the GridView and Add 2 plain asp:Parameters with default value set. On the ObjectDataSource_Selecting Event you set these parameters through e.InputParameters[]
Hope this helps.