How to Sort the Gridview Based Session values - asp.net

I have a gridview with hyperlink if i click on hyperlink the values going to button of the gridview.
protected void gvDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.DataItem != null)
{
// DataTable readDt = null;
List<String> rows = null;
//Fetching the List from Session Object
if (Session["ReadJobDetails"] != null)
{
rows = (List<String>)Session["ReadJobDetails"];
}
if (rows.Contains(DataBinder.Eval(e.Row.DataItem, "JobTitle").ToString()))
{
//since we used GridView level CSS class, individual CSS class is not applying, so added manually
//e.Row.BackColor = System.Drawing.Color.FromArgb(208, 217, 206);
e.Row.BackColor = System.Drawing.Color.Silver;
e.Row.ForeColor = System.Drawing.Color.White;
e.Row.Font.Bold = false;
}
else
{
e.Row.BackColor = System.Drawing.Color.FromArgb(247, 247, 247);
e.Row.ForeColor = System.Drawing.Color.Black;
e.Row.Font.Size = 9;
e.Row.Font.Bold = true;
}
}
}
catch (Exception)
{
throw;
}
}

Related

How to paint cells in row (Telerik)?

I've next code that handle fowFormatting my cells:
private void gridViewRaces_RowFormatting(object sender, RowFormattingEventArgs e)
{
foreach (var cellColumn in e.RowElement.Data.Cells)
{
var cellInfo = cellColumn as GridViewCellInfo;
if (cellInfo != null)
{
cellInfo.Style.DrawFill = true;
if (cellInfo.ColumnInfo.Name == "columnContactProducerName")
{
cellInfo.Style.DrawFill = true;
cellInfo.Style.BackColor = Color.Yellow;
}
else if (cellInfo.ColumnInfo.Name == "columnTransport")
{
cellInfo.Style.BackColor = Color.Yellow;
}
else
{
cellInfo.Style.BackColor = ColorTranslator.FromHtml((e.RowElement.Data.DataBoundItem as RaceForListDto).Color);
}
}
}
//e.RowElement.BackColor = ColorTranslator.FromHtml((e.RowElement.Data.DataBoundItem as RaceForListDto).Color);
}
but my cells aren't painting. How to paint some cells in rows on dataBinding?
It looks like the proper event to do this is ItemDataBound event. See here:
http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/appearance-and-styling/conditional-formatting
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
//Is it a GridDataItem
if (e.Item is GridDataItem)
{
//Get the instance of the right type
GridDataItem dataBoundItem = e.Item as GridDataItem;
//Check the formatting condition
if (int.Parse(dataBoundItem["Size"].Text) > 100)
{
dataBoundItem["Received"].ForeColor = Color.Red;
dataBoundItem["Received"].Font.Bold = true;
//Customize more...
}
}
}
Or event better is to use a custom CSS class so that you can later make changes without having to rebuild the project:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e){
if (e.Item is GridDataItem)
{
GridDataItem dataItem = e.Item as GridDataItem;
if (dataItem["Country"].Text == "Mexico")
dataItem.CssClass = "MyMexicoRowClass";
}
}

Even though i checked, the RadioButtonlist item it is taking null value. here is my code help me

I assigned radiobuttonlist items programatically. i want the radiobuttonlist items are checked/not if checked i want the checked item to compare with another value,if equal increment score else do nothing i have written a code,but while debugging even though i checked the item it is taking null value please help me..
public void assign()
{
Label2.Text = ques[(int)ViewState["value"]];
RadioButtonList1.Items.Clear();
RadioButtonList1.Items.Add(new ListItem(opt1[(int)ViewState["value"]], "0"));
RadioButtonList1.Items.Add(new ListItem(opt2[(int)ViewState["value"]], "1"));
RadioButtonList1.Items.Add(new ListItem(opt3[(int)ViewState["value"]], "2"));
RadioButtonList1.Items.Add(new ListItem(opt4[(int)ViewState["value"]], "3"));
btn_Submit.Visible = false;
}
public void radiochecked()
{
if (ViewState["index"] == null)
{
ViewState["index"] = 0;
}
crctans = (string[])Session["correctanswer"];
if (RadioButtonList1.SelectedIndex > -1)
{
if (crctans[(int)ViewState["index"]] == RadioButtonList1.SelectedItem.Text)
{
score = score + 1;
}
}
ViewState["index"] = (int)ViewState["index"] + 1;
if ((int)ViewState["index"] == 10)
{
Session["result"] = score;
}
}
protected void btn_Next_Click(object sender, EventArgs e)
{
radiochecked();
}

asp.net clear row selection for Multiple Grid with Single Button Click

Hi Friends i have created a single asp.net page(c#) with multiple grid view to display records. I have multiple command button to show records in these grid.
I am using single button to clear the row selection in grid but it is not working.
Please help me...
protected void btnClearGridSelection_Click(object sender, EventArgs e)
{
if (GridView1.Visible == true)
{
GridView1.SelectedIndex = -1;
}
if (GridView1.Visible == true)
{
GridViewSearch.SelectedIndex = -1;
}
if (GridView1.Visible == true)
{
GridViewState.SelectedIndex = -1;
}
if (GridView1.Visible == true)
{
GridViewDistrict.SelectedIndex = -1;
}
if (GridView1.Visible == true)
{
GridViewType.SelectedIndex = -1;
}
if (GridView1.Visible == true)
{
GridViewEmployee.SelectedIndex = -1;
}
}
//---------------------updating my question with new problem-------------
Now i am able to to clear the selection from row but facing new problem.......When i am clicking on show(to show results state wise) button it is showing results and also clearing the selection when clicking on clear selection button with no problems....but when i click on another show button(to show results district wise) to show results it is showing results but when i select the row and click on clear selection button it is showing both grid with records......please help how to hide state Grid when i am Clicking on Clear Selection Button while using District grid......i am posting the code which i am using on Button Click....
.......clear selection....Button click:
protected void btnClearGridSelection_Click(object sender, EventArgs e)
{ GridView1.SelectedIndex = -1;
GridViewSearch.SelectedIndex = -1;
GridViewState.SelectedIndex = -1;
GridViewDistrict.SelectedIndex = -1;
GridViewType.SelectedIndex = -1;
GridViewEmployee.SelectedIndex = -1;
}
.......State Grid....Button click:
protected void btnState_Click(object sender, EventArgs e)
{
GridView1.Visible = false;
//GridViewState.Visible = true;
GridViewDistrict.Visible = false;
GridViewSearch.Visible = false;
GridViewType.Visible = false;
GridViewEmployee.Visible = false;
btnClearGridSelection.Visible = true;
string d2 = ddlState.Text;
string strquery = "select * from tblAsset2 where v_State=#d2";
if (con.State != ConnectionState.Closed)
{
con.Close();
}
con.Open();
try
{
SqlCommand cmd = new SqlCommand(strquery, con);
cmd.Parameters.AddWithValue("#d2", d2);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridViewState.DataSource = ds;
GridViewState.DataBind();
int rowCount = GridViewState.Rows.Count;
if (rowCount <= 0)
{
Label67.Visible = true;
GridViewState.Visible = false;
Label67.Text = "Sorry!....Records not found.";
}
else
{
GridViewState.Visible = true;
Label67.Visible = false;
}
}
catch (Exception ex)
{
Response.Write(ex);
}
finally
{
con.Close();
}
}
....................District Grid....Button click:
protected void btnDistrict_Click(object sender, EventArgs e)
{
GridView1.Visible = false;
GridViewDistrict.Visible = false;
//GridViewDistrict.Visible = true;
GridViewSearch.Visible = false;
GridViewType.Visible = false;
GridViewEmployee.Visible = false;
btnClearGridSelection.Visible = true;
string d2 = ddlDistrict.Text;
string strquery = "select * from tblAsset2 where v_District=#d2";
if (con.State != ConnectionState.Closed)
{
con.Close();
}
con.Open();
try
{
SqlCommand cmd = new SqlCommand(strquery, con);
cmd.Parameters.AddWithValue("#d2", d2);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridViewDistrict.DataSource = ds;
GridViewDistrict.DataBind();
int rowCount = GridViewDistrict.Rows.Count;
if (rowCount <= 0)
{
Label67.Visible = true;
GridViewDistrict.Visible = false;
Label67.Text = "Sorry!....Records not found.";
}
else
{
GridViewDistrict.Visible = true;
Label67.Visible = false;
}
}
catch (Exception ex)
{
Response.Write(ex);
}
finally
{
con.Close();
}
}
I resolved my problem and it may help someone else so i am posting it.. i have written wrong line:- GridViewState.Visible = true; // i have commented this line and error gone
protected void GridViewState_RowCreated(object sender, GridViewRowEventArgs e)
{
GridViewState.Visible = true; // i have commented this line and error gone
}

Is this the best/correct way to use the DevExpress ASPxGridView to edit a List<T>?

Would someone who knows DeveExpess ASPxGridView take a look at this.
Is this the best/correct way to use the grid to edit a List?
I have an object of Type ItemModel and the code below is used to allow an ASP.NET Web Site user to do CRUD operations on list of ItemModels.
My problem is that it seems too complex and I suspect I am not taking good advantage of the Grid.
For simplicity, I have left the Database Access Code that will load and save the list of ItemModels.
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
BindGrid();
}
protected void OnRowInserting(object sender, ASPxDataInsertingEventArgs e)
{
IList<ItemModel> itemModels = CachedModels;
int lineID = 0;
if (itemModels .Count > 0)
{
lineID = itemModels[itemModels.Count - 1].LineID + 1;
}
ItemModel itemModel = new ItemModel()
{
, Code = e.NewValues["Code"] == null ? string.Empty : e.NewValues["Code"].ToString()
, Name = e.NewValues["Name"] == null ? string.Empty : e.NewValues["Name"].ToString()
, DateCreated = DateTime.Now
, DateUpdated = DateTime.Now
};
itemModels.Add(itemModel);
CachedModels = itemModels;
ASPxGridView aspxGridView = (ASPxGridView)sender;
aspxGridView.CancelEdit();
e.Cancel = true;
BindGrid();
}
protected void OnRowUpdating(object sender, ASPxDataUpdatingEventArgs e)
{
IList<ItemModel> itemModels = CachedModels;
int lineID = Convert.ToInt32(e.Keys[0].ToString());
ItemModel itemModel = null;
foreach (ItemModel model in itemModels)
{
if (model.LineID == lineID)
{
itemModel = model;
break;
}
}
if (itemModel != null)
{
itemModel.Code = e.NewValues["Code"] == null ? string.Empty : e.NewValues["Code"].ToString();
itemModel.Name = e.NewValues["Name"] == null ? string.Empty : e.NewValues["Name"].ToString();
containerItemModel.DateUpdated = DateTime.Now;
itemModels[lineID] = itemModel;
CachedModels = itemModels;
}
ASPxGridView aspxGridView = (ASPxGridView)sender;
aspxGridView.CancelEdit();
e.Cancel = true;
BindGrid();
}
protected void OnRowDeleting(object sender, ASPxDataDeletingEventArgs e)
{
IList<ItemModel> itemModels = CachedModels;
int lineID = Convert.ToInt32(e.Keys[0].ToString());
ItemModel itemModel = null;
foreach (ItemModel model in itemModels)
{
if (model.LineID == lineID)
{
itemModel = model;
break;
}
}
if (itemModel != null)
{
itemModels.Remove(itemModel);
CachedModels = itemModels;
}
ASPxGridView aspxGridView = (ASPxGridView)sender;
aspxGridView.CancelEdit();
e.Cancel = true;
BindGrid();
}
private void BindGrid()
{
grdItems.DataSource = CachedModels;
grdItems.DataBind();
}
private IList<ItemModel> CachedModels
{
get
{
List<ItemModel> models= (List<ItemModel>)Session["ItemModels"];
if (models == null)
{
models= new List<ItemModel>();
Session["ItemModels"] = models;
}
return models;
}
set
{
Session["ItemModels"] = value;
}
}
I believe your current code is quite correct.
Here is the suggested way from DX:
ASPxGridView - How to implement CRUD operations with a custom data source

Bind Dropdownlist with optGroup from sql datasource

I have to bind a Dropdownlist with coutries which should be grouped by region, I have found a sample code from the following link,
http://www.codeproject.com/KB/custom-controls/DropDownListOptionGroup.aspx?msg=3984074#xx3984074xx
I wanted the country list same as this. But problem is that I want to bind the dropdownlist from sql result. I have tried the following but didn't work,
ddlCountry.DataSource = CountryDtoCollection;
ddlCountry.DataBind();
ddlCountry.Attributes.Add("OptionGroup", "Region");
any one knows any solution for this.
you can write a custom server control and use the data source witch containing the text and region separated by | then split it when use.
[ToolboxData("<{0}:CustomDropDownList runat=server></{0}:CustomDropDownList>")]
public class CustomDropDownList : DropDownList
{
protected override void RenderContents(HtmlTextWriter writer)
{
if (this.Items.Count > 0)
{
bool selected = false;
bool optGroupStarted = false;
string lastOptionGroup = string.Empty;
for (int i = 0; i < this.Items.Count; i++)
{
ListItem item = this.Items[i];
if (item.Enabled)
{
if (lastOptionGroup != item.Text.Split("|")[1])
{
if (optGroupStarted)
{
writer.WriteEndTag("optgroup");
}
lastOptionGroup = item.Text.Split("|")[1];
writer.WriteBeginTag("optgroup");
writer.WriteAttribute("label", lastOptionGroup);
writer.Write('>');
writer.WriteLine();
optGroupStarted = true;
}
writer.WriteBeginTag("option");
if (item.Selected)
{
if (selected)
{
this.VerifyMultiSelect();
}
selected = true;
writer.WriteAttribute("selected", "selected");
}
writer.WriteAttribute("value", item.Value, true);
if (item.Attributes.Count > 0)
{
item.Attributes.Render(writer);
}
if (this.Page != null)
{
this.Page.ClientScript.RegisterForEventValidation(this.UniqueID, item.Value);
}
writer.Write('>');
HttpUtility.HtmlEncode(item.Text.Split("|")[0], writer);
writer.WriteEndTag("option");
writer.WriteLine();
}
}
if (optGroupStarted)
{
writer.WriteEndTag("optgroup");
}
}
}
}

Resources