Gridview SelectedIndexChanged can not find cell value - asp.net

I followed this article for key press search in gridview.Everything working fine but I can't find selecterow cell vaule using selectedindexchanged. Still I can fireup the method but can't see value it return only &nbsp.
http://www.aspsnippets.com/Articles/Search-GridView-with-Paging-on-TextBox-KeyPress-using-jQuery-in-ASPNet.aspx

If you using BoundField Column, then
string name = YourGridView.SelectedRow.Cells[0].Text;
and for TemplateField Column controls
string name = (YourGridView.SelectedRow.FindControl("lblSomeName") as Label).Text;

Nothing worked. However I get the value by using java script
$("#<%=gvCustomers.ClientID%> tr:has(td)").click(function (e) {
var cell = $(this).find("td:eq(0)");
$(this).closest('table').find('th').eq($(this).index());
});

Related

How can i select Datakey value of a grid

How can i select Datakey value of a grid .? I tried but only getting inside selected index changed event.
[In my application there is a grid.In that grid there link buttons.My issue is when i am clicking on link button i want to acceess value in Datakeynames. is there any method to access value in Datakeynames. or is there any other property for grid to keep key values ].Please help
You have your answer at below link
How to get the Command Argument on Row Data bound Event of gridview
Happy coding!!!
Try like this,
Assign your Value in the Command Argument.
In RowCommand Event,
if (e.CommandName == "Link")
{
string Value = e.CommandArgument.ToString();
}

DataBind of DropDownList works, but does not show in code behind

I'm working on web pages that have an ASP DropDownList defined, and on page load the data source is bound to it through DataSource.DataBind(). When I step through the code, the drop down list does not show anything in it, but when the page actually displays, it does have items in the list. When exactly does DataBind() get applied to the control?
The problem is that some of the values returned by the SQL back end have a null Text, so nothing is displayed in the drop down list for that row. I want to just use the Value as the Text, but only if the Text is null. And when I put the code to loop through and do that right after the DataBind(), there is nothing in the drop down list at that point in the code.
Thanks!
The best option for your specific example is to do what kd7 said, and modify your query. However, if you ever need to do other modifications to the items, you can access them in the DataBound event of the DropDownList. MSDN. This event fires after the binding occurs.
You could
a) Modify your database query to exclude null values
or
b) Before you databind, iterate through the data returned and remove the undesired values.
You can just use LINQ on the data before you assign it to create a new type on the fly with the data arranged how you want. Here is a quick example:
// This simulates the data you get from the DB
List<SomeObject> lstData = new List<SomeObject>();
lstData.Add(new SomeObject() { ID = "1", Text = "One" });
lstData.Add(new SomeObject() { ID = "2", Text = "" });
lstData.Add(new SomeObject() { ID = "3", Text = "Three" });
// This will take your data and create a new version of it substituting the
// ID field into the Text field when the Text is null or empty.
yourDropDownList.DataSource = lstData.Select(i => new {
ID = i.ID,
Text = string.IsNullOrEmpty(i.Text) ? i.ID : i.Text }).ToList();
// Then just databind
yourDropDownList.DataBind();
The DropDownList would have the following items:
One
2
Three

Binding LinqDataSource from code-behind to Gridview

i have a grdidview control on the .aspx page and i am trying to connect dynamically from code behind and bind the gridview but somehow it throwing me an error... what is wrong with this code? any help?
LinqDataSource LDS_POReport = new LinqDataSource();
LDS_POReport.ContextTypeName = "DataContextDataContext";
LDS_POReport.Selecting += new EventHandler<LinqDataSourceSelectEventArgs>(LinqDataSourcePO_Selecting);
this.gvReport.DataSource = "LDS_POReport";
//this.gvReport.DataBind();
Update:
after i update the code to
this.gvReport.DataSource = LDS_POReport;
it works fine but when i try to sort i get this error:
The GridView 'gvReport' fired event Sorting
which wasn't handled.
i added this but no effect.
LDS_POReport.AutoPage = true;
LDS_POReport.AutoSort = true;
I guess that your problem is here:
this.gvReport.DataSource = "LDS_POReport";
The above code line attempts to assign a string to a property that expects some sort of data source. I assume that you really intended to assign the LinqDataSource object itself:
this.gvReport.DataSource = LDS_POReport;
First thing, the DataSource should get a reference to the object containing the data, not the name of the object containing the data. GridViews can work reflectively, but not THAT reflectively.

ASP.NET EnqityDataSource WhereParameters, creates new property

I am trying to populate GridView, using EntityDataSource(code behind), I need to able to sort GridView. However when I sort i get error:
A property with name 'aspnet_Users.UserId1' does not exist in metadata for entity type
So I beleive it is because I generate where parameter in code behind:
ActiveEnqDataSource.WhereParameters.Add(new SessionParameter("aspnet_Users.UserId", TypeCode.Object, "UserName"));
Full code is :
ActiveEnqDataSource.ConnectionString = db.Connection.ConnectionString;
ActiveEnqDataSource.DefaultContainerName = "Entities";
ActiveEnqDataSource.EntitySetName = "Enquiries";
ActiveEnqDataSource.Include = "UserCars.CarModel.CarMake, Category, aspnet_Users";
ActiveEnqDataSource.EnableUpdate = true;
ActiveEnqDataSource.EnableInsert = true;
ActiveEnqDataSource.EnableDelete = true;
ActiveEnqDataSource.AutoGenerateWhereClause = true;
ActiveEnqDataSource.WhereParameters.Add(new SessionParameter("aspnet_Users.UserId", TypeCode.Object, "UserName"));
Any suggestions? Thank you very much! The gridview itself renders perfectly, only thing I cannot sort it, any "whereParameters" I add, Add 1 to the property e.g UserId1,EnquiryStatus1, ProdauctName1. etc...
I got a similar error because I was adding the where clause each time the page was posted back. Dropping my code that generated the where clause inside an IsPostback statement fixed the problem:
if (!IsPostBack) {
// code to add where parameters
}
I got the same error when I used markup to define a Where parameter AND then I added the same parameter in code. Somewhere along the line, the 1 at the end of the parameter name was added.

Retain the value entered in the cell of DevExpress Xtragrid

I am using DevExpress Xtragrid control in my C#.net windows application.
I enter some value into the first cell of the grid and if i go to second cell , the value entered in the first cell disappears.
How to retain the value entered in the cell ?
I am assuming that you are using this for an unbound column in a gridView (Xtragrid), first step is make sure to go to the column properties, and change the UnboundType property value to the datatype that you will be entering into that column, example below uses double.
Assign the CustomUnboundColumnData event to your gridView. Make sure that you declare a class level variable (named _userEnteredData in code sample below) to hold the value that you are entering into your gridView, then add the following piece of code, but make sure that you change the names to match your gridView and variable names:
Class level variable declaration:
private double _userEnteredData = 0;
Now the event:
private void gridView1_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
{
if (e.Column == gridColumn_YourColumn && e.IsSetData)
{
_userEnteredData = Convert.ToDouble(e.Value);
}
else if (e.Column == gridColumn_YourColumn && e.IsGetData)
{
e.Value = _userEnteredData;
}
}
I hope this helps.
You can get further details from here:
http://documentation.devexpress.com/#WindowsForms/CustomDocument1477
Few possibilities:
check FieldName property of edited column. Maybe there is a typo, so grid does not pass your entered value to underlying datasource
property that is bound to column must have public setter. If there is only getter, grid also won't be capable to store entered value
check ColumnOptions.ReadOnly property in grid column - must be set to false
Hope this helps

Resources