I am retrieving data from SQL Server to a grid view in ASP.Net. Below is the header of the grid view after I retrieved the data from the database.
Time| Atlanta_1| Atlanta_2| Atlanta_3|
I want to replace the name of the cities with "City" dynamically e.g.
Time| City_1| City_2| City_3|
So basically I want to replace a part of the header with "City" and I want to do it while binding it in a grid view NOT in SQL Server. Below is the code how I am retrieving data from the database to my gridview
SqlConnection con = new SqlConnection("My Connection");
string s = "My Stored Procedure";
con.Open();
SqlDataAdapter da = new SqlDataAdapter(s, con);
DataSet ds = new DataSet();
da.Fill(ds);
gridView1.DataSource = ds;
gridView1.DataBind();
con.Close();
You can use the RowDataBound event. Then loop all the Header Cells. Check the Cell for the _ and replace with the correct value. If it is always Atlanta you can just do a Replace.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
foreach (TableCell cell in e.Row.Cells)
{
if (cell.Text.Contains("_"))
{
cell.Text = "City_" + cell.Text.Split('_')[1];
}
}
}
}
Related
I have a combo box and a web service. I want to call web service and then when I open my combo I see the values the are in my web service.My code did not work.Can anybody help me?
protected void Page_Load(object sender, EventArgs e)
{
string firstParam = "1";
DataSet ds2 = ws1.Automation_H022_MamooriyatSaati_View(firstParam, secondParam, thirdParam);
this.GridView1.DataSource = ds2;
foreach (System.Data.DataTable table1 in ds1.Tables)
{
foreach (DataRow dr in table1.Rows)
{
cmbox.DataTextField = "Tp_Place";
cmbox.DataValueField = "Lg_Personality";
cmbox.DataBind();
cmbox.Items.Insert(0, new ListItem("", "1"));
}
}
}
You are adding not adding anything to the cmbox in the foreach (DataRow dr in table1.Rows). And even if you did the next loop would overwrite it.
You need to set the datasource for the ComboBox in the same way you set the GridView.
If there is more than one table in ds1 and you want both tables to be visible in the combobox then you need to merge those two (or more) tables first and then bind them as a single source of data.
DataSet ds2 = ws1.Automation_H022_MamooriyatSaati_View(firstParam, secondParam, thirdParam);
this.GridView1.DataSource = ds2;
cmbox.DataSource = ds1.Tables[0];
cmbox.DataTextField = "Tp_Place";
cmbox.DataValueField = "Lg_Personality";
cmbox.DataBind();
cmbox.Items.Insert(0, new ListItem("", "1"));
I have a gridview in asp.net, in which I am inserting datas. When I insert repeated value then it will show item repeated. Now I need to show after the item repeated alert message the cursor will focus on the row value contain the item which is repeated. If my data table already contain code C1, then I again type c1 for insert then the cursor will focus on the row which contain c1 in gridview. Here is my code
protected void Button15_Click(object sender, EventArgs e)
{
Control control = null;
if (GridView1.FooterRow != null)
{
control = GridView1.FooterRow;
}
else
{
control = GridView1.Controls[0].Controls[0];
}
string Code = (control.FindControl("txtcode") as TextBox).Text;
string txtno= (control.FindControl("txtno") as TextBox).Text;
using (SqlConnection con = new SqlConnection("Data Source=XXXXXX;Initial Catalog=XXXXXX;User ID=XXXX;Password=XXXXXX"))
{
using (SqlCommand cmd = new SqlCommand())
{
DataTable dt = new DataTable();
SqlDataAdapter da1;
da1 = new SqlDataAdapter("select code from tbltmp where code='" + Code + "' ", con);
da1.Fill(dt);
if (dt.Rows.Count > 0)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(),
"alertMessage",
"alert('Item Repeated');", true);
(control.FindControl("txtcode") as TextBox).Focus();
}
else
{
(control.FindControl("txtno") as TextBox).Focus();
}
}
}
}
set focus on the textbox as textbox1.focus in button event of repeated row checking
Here's my code:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
connection db_obj1 = new connection();
SqlConnection sql_obj = db_obj1.Connect();
if (this.DropDownList1.SelectedIndex >= 0)
{
string brand = DropDownList1.Items[DropDownList1.SelectedIndex].ToString();
string query = "Select Product,Model, NetPrice, Cost, Profit from products where Brand='" + brand + "'";
// SqlDataReader query_read = query.ExecuteReader();
SqlDataAdapter adapter = new SqlDataAdapter();
SqlCommand command = new SqlCommand(query, sql_obj);
// cmd.Parameters.AddWithValue("#tab",);
DataTable table = new DataTable();
adapter.SelectCommand = command;
adapter.Fill(table);
// Response.Write(dt.Rows.Count.ToString());
GridView grd = new GridView();
grd.FindControl("GridView1");
grd.DataSource = table;
grd.DataBind();
}
}
The problem is that when I run the code, I only see the drop down list and no GridView even when I select dofferent options. I tried debugging and it seems that the table DOES get filled and the only problem that I think is in the line 'grd.FindControl("GridView1").Is this the proper way to give a gridview a data source?
According to me you should not use like
string brand = DropDownList1.Items[DropDownList1.SelectedIndex].ToString();
on behalf that try as below
string brand = DropDownList1.SelectedItem.Text;
after that fire a query.
i hope it will halpful.
After Edit now use this code
string query = "Select Product,Model, NetPrice, Cost, Profit from products where Brand='" + brand + "'";
SqlDataAdapter adapter = new SqlDataAdapter();
SqlCommand command = new SqlCommand(query, sql_obj);
// cmd.Parameters.AddWithValue("#tab",);
DataSet ds = new DataSet();
DataTable table;
adapter.SelectCommand = command;
adapter.Fill(ds, "ABC");
table = ds.Tables["ABC"];
// Response.Write(dt.Rows.Count.ToString());
GridView grd = new GridView();
grd.FindControl("GridView1");
grd.DataSource = table;
grd.DataBind();
Explanation
adapter.Fill(ds, "ABC");
here ABC is a temp name of table you can take anything.
table = ds.Tables["ABC"];
and write here same table name as you have taken a temp table name.
now try it.
I am trying to learn how to use sql data adapter ... I have coded the following to check how it works ...
the problem is i want to retrieve values of 3 columns(DeptNo,DeptId,DeptName) of my database table "Sana" separately and display them in three separate text boxes ...
Through the code mentioned below I am able to retrieve the value of entire tuple of data base table together
what should I do to reach above mentioned result???
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection connect = new SqlConnection(ConfigurationManager.ConnectionStrings["TestConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand("Select DeptNo,DeptId,DeptName from Sana where DeptName='" + TextBox1.Text + "'", connect);
SqlDataAdapter myAdapter = new SqlDataAdapter(cmd);
DataSet MyDataSet = new DataSet();
myAdapter.Fill(MyDataSet, "Departments");
object[] rowVals = new object[3];
foreach (DataTable myTable in MyDataSet.Tables)
{
foreach (DataRow myRow in myTable.Rows)
{
foreach (DataColumn myColumn in myTable.Columns)
{
Response.Write(myRow[myColumn] + "\t");
}
}
}
}
}
foreach (DataRow myRow in MyDataSet.Tables[0].Rows)
{
TextBox1.Text = myRow["DeptNo"].ToString();
TextBox2.Text = myRow["DeptId"].ToString();
...
}
I've created a listview that's filled up with a list of guitars from the database on page load like so:
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["GuitarsLTDBConnectionString"].ToString());
SqlCommand cmd = new SqlCommand("SELECT * FROM Guitars", con);
SqlDataAdapter da = new SqlDataAdapter(cmd.CommandText, con);
DataTable dt = new DataTable();
da.Fill(dt);
lvGuitars.DataSource = dt;
lvGuitars.DataBind();
}
The following code filters that list of guitars by a certain Make when the user checks the checkbox corresponding to that make
protected void chkOrgs_SelectedIndexChanged(object sender, EventArgs e)
{
DataTable dt = (DataTable)lvGuitars.DataSource;
DataView dv = new DataView(dt);
if (chkOrgs.SelectedValue == "Gibson")
{
dv.RowFilter = "Make = 'Gibson' OR Make='Fender'";
}
lvGuitars.DataSource = dv.ToTable();
lvGuitars.DataBind();
}
Now, what I want to do is be able to sort the latest data that is present within the listview. Meaning, if sort is clicked before filtering, the it should sort all data. If sort is clicked after filtering, it should sort the filtered data. I'm using the following
code, which is triggered upon a LinkButton click
protected void lnkSortResults_Click(object sender, EventArgs e)
{
DataTable dt = (DataTable)lvGuitars.DataSource;
DataView dv = new DataView(dt);
dv.Sort = "Make ASC";
lvGuitars.DataSource = dv.ToTable();
lvGuitars.DataBind();
}
The problem is that all the data that the listview was loaded with before any filtering is sorted, and not the latest filtered data. How can I change this code so that the latest data available in the listview is the one that's sorted?
Thanks
if (!Page.IsPostBack)
{
SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["GuitarsLTDBConnectionString"].ToString());
SqlCommand cmd = new SqlCommand("SELECT * FROM Guitars", con);
SqlDataAdapter da = new SqlDataAdapter(cmd.CommandText, con);
DataTable dt = new DataTable();
da.Fill(dt);
lvGuitars.DataSource = dt;
lvGuitars.DataBind();
ViewState["dt"] = dt;
}
The loading need to be set like this. Access/Update the Viewstate as well on filter and Sorting.