How to select some data from DataTable - asp.net

I want to search some data from DataTable to show in GridView.
Like
(select * from customer where id="1")
Can I do that?

DataRow[] foundRows = yourTable.Select("id=1");
Or you can filter rows in default view:
yourTable.DefaultView.RowFilter = "id = 1";
GridView gv = new GridView();
gv.DataSourse = yourTable.DefaultView
gv.DataBind()

Related

How to bind gridview with some conditions in asp.net?

DataTable dtbind = new DataTable();
dtbind = objvehicleBAL.GetTaxdetails();
for (int i = 0; i < dtbind.Rows.Count; i++)
{
DateTime dt1 = DateTime.ParseExact(dtbind.Rows[i]["todate"].ToString(), "dd/MM/yyyy", null);
if (dt1 < ((DateTime.Now.AddDays(15))))
{
GVTax.DataSource = dtbind.Rows[i];
GVTax.DataBind();
}
}
I had written my conditions in if(). I want to bind only satisfied rows in grid. How can I write this?
You do not need to bind the Grid in loop on the Row of data table rather filter the DataTable by the condition you want and bind it once. You can get DataView from the data table and use its property DataView.RowFilter to apply the date filter.
dtbind = objvehicleBAL.GetTaxdetails(); //Filter the record in GetTaxdetails
DataView dv = dtbind.DefaultView; //or use DataView with RowFilter
dv .RowFilter = "todate = #" + DateTime.Now.AddDays(15).ToString() + "#";
GVTax.DataSource = dv;
GVTax.DataBind();
DataTable dtbind1 = objvehicleBAL.GetTaxdetails();
DataTable dtbind2 = new DataTable();
foreach (DataRow row in dtbind1.Rows)
{
DateTime dt1 = DateTime.ParseExact(row["todate"].ToString(), "dd/MM/yyyy", null);
if (dt1 < ((DateTime.Now.AddDays(15))))
dtbind2.Rows.Add(row);
}
}
GVTax.DataSource = dtbind2;
GVTax.DataBind();
No need to bind each row and call DataBind mehtod each time.
Just use the following:
protected void BindGrid()
{
DataTable dtbind = new DataTable();
dtbind=objvehicleBAL.GetTaxdetails();//get the rows filtered in SQL
if(dtbind!=null && dtbind.Rows.Count>0)//always check for null for preventing exception
{
GVTax.DataSource = dtbind;
}
GVTax.DataBind();
}
Hope this helps you!
You can use the Select method of DataTable along with a filtering expression, to get the rows which match your criteria. Then, bind it to to your GridView.
string filterExp = "todate < dateadd(day,15,getdate())";
var filtered = dtBind.Select(filterExp);
GVTax.DataSource = filtered ;
GVTax.DataBind();
You can create another datatable and fill the rows satisfying your condition in the second datatable and bind your gridview with second datatable (having filtered rows)
dttableNew = dttableOld.Clone();
foreach (DataRow drtableOld in dttableOld.Rows)
{
if (/*put some Condition */)
{
dtTableNew.ImportRow(drtableOld);
}
}
GVTax.DataSource = dtTableNew;
GVTax.DataBind();

Get Selected index Change Event for Each row from grid view

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.

Bind Value From XMLWebService to Dropdownlist in ASP.Net

How to bind data from an XMLWebService to a DropDownList?
I have this code but it doesn't work:
Dim xMaster As New MasterService.Masterservice
DT = xMaster.GetDataProgram
DT.Load(DT)
DDL.DataSource = DT
DDL.DataTextField = DT.Columns("cdProgram").ColumnName.ToString()
DDL.DataValueField = DT.Columns("nmProgram").ColumnName.ToString()
DDL.DataBind()
I tried to bind it to the GridView and the values are shown. But, when I tried to bind it to the dropdownlist, it returns nothing. Thank you very much.
Try this:
DDL.DataTextField = "cdProgram"
DDL.DataValueField = "nmProgram"

ASP.net Insert blank rows into Gridview

I have a gridview that is bound to a sqldatasource. The Gridview only has a pagesize of 10 and I would like each page to have 10 rows. Therefore if only 5 data rows exist then I would like to add an additional 5 empty rows. Is this easy to do?
Fill your data into data set and count the number of rows retrieved then fill the remaining to the dataset with empty dataRows try this:
Suppose you have a DataSet dt filled with the table or data you want
int remainingRows=10 - dt.Rows.Count;
DataRow dr;
for (int i = 0; i < remainingRows; i++)
{
dr = dt.NewRow();
dr[0] = dr[1] = dr[2] = dr[3] = dr[4] = "";//index goes the no of cols in the table
dt.Rows.Add(dr);
}
dt.AcceptChanges();
grdView.DataSource = dt;
grdView.DataBind();
You can see this
For meeting your requirement , I think you shouldn't use sqldatasource to bind gridview instead of manual binding the datasource to gridview. you can encapsulate a datatable or dataview which each page have 10 rows as the datasource.

sort dropdownlist by date, after it is populated from datasource

I want to sort the dropdownlist by date, but i cant figure out how.
ddate.DataSource = myTable
ddate.DataTextField = "ddate7"
ddate.DataValueField = "ddate7"
ddate.DataBind()
If myTable is a DataTable then you could put it into a Dataview and sort it there like this:
Dim dv As New DataView(myTable)
dv.Sort = "ddate7"
ddate.DataSource = dv
ddate.DataTextField = "ddate7"
ddate.DataValueField = "ddate7"
ddate.DataBind()
You can use a DataView to sort and filter the DataTable you may try the following code,
DataView dv = new DataView(myTable);
dv.Sort = "ddate7 ASC";
ddate.DataSource = dv;
ddate.DataTextField = "ddate7";
ddate.DataValueField = "ddate7";
ddate.DataBind();
Good luck.

Resources