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
Related
here the code which i am using to import excel to grid view.. Each time getting error at dt.Rows.Add()
here the error "The string was not recognized as a valid DateTime. There is an unknown word starting at index 0.Couldn't store in SUPPLY DATE Column. Expected type is DateTime."
in .aspx page i don't defined any column with datetime
DataTable dtExl = conUpLoad.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string getExcelSheetName = dtExl.Rows[0]["Table_Name"].ToString();
OleDbCommand ExcelCommand = new OleDbCommand("Select * From [" + getExcelSheetName + "]", conUpLoad);
OleDbDataAdapter ExcelAdapter = new OleDbDataAdapter(ExcelCommand);
DataSet ExcelDataSet = new DataSet();
ExcelAdapter.Fill(ExcelDataSet);
//conUpLoad.Close();
// Clearing the grid ::::::::::::::::::::::
ViewState["dt"] = null;
GridView1.DataSource = ViewState["dt"] as DataTable;
GridView1.DataBind();
// ::::::::::::::::::::::::::::::::::::::::
DataTable dt = ExcelDataSet.Tables[0];
int i = 0;
if (ExcelDataSet.Tables[0].Rows.Count > 0)
{
String DINumber = ExcelDataSet.Tables[0].Rows[0][0].ToString();
String PartNo = ExcelDataSet.Tables[0].Rows[0][1].ToString();
String DeliveryDt = Convert.ToDateTime(ExcelDataSet.Tables[0].Rows[0][3].ToString()).ToString("dd-MMM-yyyy");
String Location = ExcelDataSet.Tables[0].Rows[0][5].ToString();
String Qty = ExcelDataSet.Tables[0].Rows[0][7].ToString();
String PartyCode = ExcelDataSet.Tables[0].Rows[0][10].ToString();
dt.Rows.Add(DINumber, PartNo, DeliveryDt, Location, Qty, PartyCode);
ViewState["dt"] = dt;
GridView1.DataSource = ViewState["dt"] as DataTable;
GridView1.DataBind();
i++;
}
else
{
GridView1.DataSource = null;
GridView1.DataBind();
}
I had tried binding dynamically created drop down list inside grid view and also added the selected index change event for it but it is giving me output when i had change value of first row drop down. i want individual rows selected index change event what can i do?
I had taken grid view in update panel and added dynamically drop down list in grid view and also bind it for each row.now i want to add selected index change event for each rows drop down list . my code is as follows:-
for (int i = 0; i < gv_details.Rows.Count; i++)
{
DropDownList ddl_Hotel = new DropDownList();
ddl_Hotel.ID = "ddl_Hotel_" + i;
//add TextBox to the first column
string loc = gv_details.Rows[i].Cells[3].Text.ToString();
string selecctloc = "select locid from loc_master where location='" + loc + "'";
SqlDataReader dr = cdf.Execute_SelectForDR(selecctloc);
dr.Read();
string loc_id = dr["locid"].ToString();
dr.Close();
string bind_hotelList = "SELECT Hotel_Id,Hotel_Name FROM Mast_Hotel WHERE LocId ='" + loc_id + "'";
cdf.Bind_DropDownList(ddl_Hotel, bind_hotelList, "Hotel_Name", "Hotel_Id");
//ddl_Hotel.Items.Add(new ListItem("Select", "0"));
ddl_Hotel.Items.Insert(0, new ListItem("---Select---", "0"));
ddl_Hotel.SelectedValue = "0";
gv_details.Columns[5].Visible = true;
ddl_Hotel.SelectedIndexChanged += new EventHandler(ddl_Hotel_SelectedIndexChanged);
this.Form.Controls.Add(ddl_Hotel);
UpdatePanel1.ContentTemplateContainer.Controls.Add(ddl_Hotel);
AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
trigger.ControlID = ddl_Hotel.ID;
trigger.EventName = "SelectedIndexChanged";
UpdatePanel1.Triggers.Add(trigger);
ddl_Hotel.AutoPostBack = true;
gv_details.Rows[i].Cells[5].Controls.Add(ddl_Hotel);
// ddl_Hotel.Attributes.Add("onchange", "retun destination_name()");
}
please suggest me the answer.
I need the address (below the dropdown) to change as per the name that is selected in the dropdown... (they are all for one user only)...! (i donno why this keeps saying your post seems only code!!!!!!)
public DataSet BindDropDownListToAUserAddress2()
{
UserFunctions objGetSession = new UserFunctions();
string strSession = objGetSession.GetEmailFromSession();
DataSet dsUserAddress = new DataSet();
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]);
con.Open();
string strQuery = "(SELECT FirstName +' '+ LastName as FullName, * FROM AUserAddress inner join AState on AUserAddress.State_ID = AState.ID inner join ACountry on AUserAddress.Country_ID = ACountry.ID inner join AUser on AUserAddress.AUser_ID=AUser.ID where AUser.Email='" + strSession + "')";
SqlCommand cmd = new SqlCommand(strQuery, con);
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
da.Fill(dsUserAddress, "AUserAddress");
con.Close();
return dsUserAddress;
}
protected void ddlName_SelectedIndexChanged(object sender, EventArgs e)
{
DataSet dsUserAddress = objBindDDL.BindDropDownListToAUserAddress2();
string strSession = objGetSession.GetEmailFromSession();
ddlName.SelectedValue = "FullName";
if (objBindDDL.BindDropDownListToAUserAddress2() != null && objBindDDL.BindDropDownListToAUserAddress2().Tables.Count > 0)
{
lblDisplayFirstName.Text = dsUserAddress.Tables["AUserAddress"].Rows[0]["FirstName"].ToString();
lblDisplayLastName.Text = dsUserAddress.Tables["AUserAddress"].Rows[0]["LastName"].ToString();
lblDisplayAddressLine1.Text = dsUserAddress.Tables["AUserAddress"].Rows[0]["AddressLine1"].ToString();
lblDisplayAddressLine2.Text = dsUserAddress.Tables["AUserAddress"].Rows[0]["AddressLine2"].ToString();
lblDisplayAddressLine3.Text = dsUserAddress.Tables["AUserAddress"].Rows[0]["AddressLine3"].ToString();
lblDisplayCity.Text = dsUserAddress.Tables["AUserAddress"].Rows[0]["City"].ToString();
lblDisplayState.Text = dsUserAddress.Tables["AUserAddress"].Rows[0]["StateName"].ToString();
lblDisplayCountry.Text = dsUserAddress.Tables["AUserAddress"].Rows[0]["CountryName"].ToString();
lblDisplayPostalCode.Text = dsUserAddress.Tables["AUserAddress"].Rows[0]["PostalCode"].ToString();
}
}
when you are binding data to dropdown list
ddlStatus.DataTextField = "FullName";
ddlStatus.DataValueField = "Id";
onselected index change event
int ID =Convert.Toint32(ddlName.SelectedValue)
by using this ID select only this ID Record from the dataTable and display
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%;
Hi
I have created session array :
int[] a = (int[])Session["values"];
Now i have to bind this value to my gridview. I have one column (boundfield in my gridview) in gridview.
I used code to bind array to gridview as follows, but its giving last value only as i want all the values to bind :
for (int i = 0; i < a.Length; i++)
{
str = "select * from Quest_Info where Quest_id='" + a[i] + "' order by Quest_id";
SqlDataAdapter da2 = new SqlDataAdapter(str, sqlconn);
ds2 = new DataSet();
da2.Fill(ds2, "Result");
reviewgrid.DataSource = ds2;
reviewgrid.DataBind();
}
Which code will work for this?
Asp.net, c#
Thank You.
Your code may not work because you are trying to bind gridview in the for loop which keep repeating and will result to bind only one last array id in the end.
You may try to prepare query out front before binding the gridview as example below.
string questIds = string.Empty;
for (int i = 0; i < a.Length; i++)
{
if (questIds.Length > 0)
questIds += ", ";
questIds += a[i];
}
string strSQL = "select * from Quest_Info where Quest_id IN ("+ questIds + ") order by Quest_id";
SqlDataAdapter da2 = new SqlDataAdapter(str, sqlconn);
ds2 = new DataSet();
da2.Fill(ds2, "Result");
reviewgrid.DataSource = ds2;
reviewgrid.DataBind();