I would like to add hyperlinks dynamically to the cell of a specific column in my datatable. Currently my code is only showing the text.
Here is my code:
SqlConnection conn = null;
string sSQL = "";
string connString = "Datasourceetc";
sSQL = "SELECT TEST_ID FROM TEST_TABLE";
SqlDataAdapter sda = new SqlDataAdapter(sSQL, conn);
DataTable dt = new DataTable();
sda.Fill(dt);
foreach (DataRow row in dt.Rows)
{
row["TEST_ID"] = "<a href='www.google.com'>Google</a>"; //<----I only see the text!
}
GridView1.DataSource = dt;
GridView1.DataBind();
Thank you
If you're doing this through an asp:BoundField you'll want to set the HtmlEncode property to false.
Related
I have the following Code/SQL statement in my C# project but the select statement doesnt show the values what are in the column it only shows the column name why is this ?
my code
string Table = ListBox2.Text;
string column= listBox1.GetItemText(listBox1.SelectedItem);
foreach (object Tables in ListBox2.SelectedItems)
{
using (SqlDataAdapter SQLStatement = new SqlDataAdapter("SELECT '"+column+"' FROM " + table, ConnectionString))
{
DataTable dt= new DataTable();
SQLStatement.Fill(dt);
dataGridView1.DataSource = dt;
dataGridView1.Refresh();
}
}
If you use SELECT '"+column+"' FROM table_name,generated query will be something like this: SELECT "column" FROM table_name.
Please try this:
string sqlCommandStatement = string.Format("SELECT {0} FROM {1}", column,table);
using (SqlDataAdapter SQLStatement = new SqlDataAdapter(sqlCommandStatement, ConnectionString))
{
DataTable dt= new DataTable();
SQLStatement.Fill(dt);
dataGridView1.DataSource = dt;
dataGridView1.Refresh();
}
This is my SEARCH Function with gridview.I'm not able to get gridview in Form remaining all are fine.Here i added new columns two.Please help me out.thanks
private void txtSearch02()
{
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection(myStr);
SqlCommand cmd = new SqlCommand("select ItemCode,ItemName,PointsNeeded from tb_ItemRedemption where (ItemCode is null or ItemCode='" + txtkey2.Text.Trim() + "') or (ItemName is null or ItemName='" + txtkey2.Text.Trim() + "')", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
Session["ItemCode"] = dt;
con.Open();
DataSet ds = new DataSet();
sda.Fill(ds);
dt = ds.Tables[0];
con.Close();
dt.Columns.Add("Quantity");
dt.Columns.Add("TotalPoints");
// for caliculation
txtPointsNeeded.Text = dt.Rows[0]["PointsNeeded"].ToString();
//dt.Rows[0]["Quantity"].ToString();
//dt.Rows[0]["TotalPoints"].ToString();
DataRow dr;
dr = dt.NewRow();
dt.Rows.Add(dr);
// for caliculation
txtGetQuantity.Text = txtQuantity.Text;
GridView1.DataSource = dt;
GridView1.DataBind();
GridView1.Visible = true;
}
Following on from my comments regarding parameterising the query and using the 'using(){}' statement I'd start by writing something like this:
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(myStr))
{
string sql = "select ItemCode,ItemName,PointsNeeded from tb_ItemRedemption where (ItemCode is null or ItemCode = #txtkey2) or (ItemName is null or ItemName = #txtkey2)";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.Add("#txtkey2", txtkey2.Text.Trim());
SqlDataAdapter sda = new SqlDataAdapter(cmd);
//You're currently adding a blank DataTable to a session variable below as you've not
//filled it yet. I've left this incase that's what you meant to do.
Session["ItemCode"] = dt;
con.Open();
//SqlDataAdapter should be able to fill a DataTable as well as a dataset.
sda.Fill(dt);
if (dt.Rows.Count > 0) //Put a breakpoint here to check if anything is returned in dt
{
dt.Columns.Add("Quantity");
dt.Columns.Add("TotalPoints");
// for caliculation
txtPointsNeeded.Text = dt.Rows[0]["PointsNeeded"].ToString();
//Not sure what you're trying to do here. You're just adding a blank row to dt.
DataRow dr;
dr = dt.NewRow();
dt.Rows.Add(dr);
// for caliculation
txtGetQuantity.Text = txtQuantity.Text;
GridView1.DataSource = dt;
GridView1.DataBind();
GridView1.Visible = true;
}
else
{
//Code to handle nothing being returned.
}
}
If you have previous three columns (with bindvalue) and add other two columns, It would be confusing one for it.
Add all the columns to datatable again. means three and new two.
Try it. It should work.cause your code is right.
Make Sure your Auto Generated Columns is set to 'true'.
Go to properties of grid view, and change auto generated columns to true in case of false.
Go through the links for further clarification : Click here
i am trying to Bind DateTable to Datagridview that already have columns designed using Designer in VS. Source for DataTable is sql database. I am trying to do this using following code which adds only blank rows in datagridview. also i update the DataPropertyName property of a Datagridview column with the name of the column in the DataTable
SqlConnection CN = new SqlConnection(mysql.CON.ConnectionString);
dataGridView1.AutoGenerateColumns = false;
DataTable dt = new DataTable();
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
dt.Columns.Add(col.Name);
col.DataPropertyName = col.Name;
}
SqlDataAdapter sda = new SqlDataAdapter("select id,name,age,notes from test where id = '" + txtID.Text + "'", CN);
sda.Fill(dt);
dataGridView1.DataSource = dt;
please nead help to solve the error here
you can just define an empty datatable without defining columns from your datagridview to it
SqlConnection CN = new SqlConnection(mysql.CON.ConnectionString);
dataGridView1.AutoGenerateColumns = false;
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter("select id,name,age,notes from test where id = '" + txtID.Text + "'", CN);
sda.Fill(dt);
dataGridView1.DataSource = dt;
I'm having a problem with a couple of gridviews that are filled from the code behind.
When I fill and databind GridView1 on it's own it works fine.
I then added the code to fill and bind GridView2 which displays but GridView1 dissapears completely.
If I comment out GridView2.DataBind() then GridView1 then appears again.
I can't work out what is going on.
Incidentaly if I change GridView2 for a DropDownList or a CheckBoxList then the same problem occurs, but if I change it for a ListBox then GridView1 appears.
protected void Page_Load(object sender, EventArgs e)
{
Int32 chaID = 20;
Int32 slots = 14;
String ConnectionString = WebConfigurationManager.ConnectionStrings["horizonConnectionString"].ToString();
String selectSQL = "SELECT chassis.ChassisName, srv.ChassisPosition, srv.ServerName, srv.ChassisID, srv.LocationID, chassis.LocationID AS ChaLocationID FROM srv INNER JOIN chassis ON srv.ChassisID = chassis.ChassisID WHERE (srv.ChassisID = '" + chaID + "') ORDER BY chassis.ChassisName, srv.ChassisPosition";
SqlConnection con = new SqlConnection(ConnectionString);
SqlCommand cmd = new SqlCommand(selectSQL, con);
DataTable dt2 = new DataTable();
DataView dv = new DataView();
try
{
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(selectSQL, con);
sda.Fill(dt2);
dv = dt2.DefaultView;
}
catch (Exception)
{
}
try
{
int searchIndex;
dv.Sort = "ChassisPosition";
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Bay", typeof(Int32)));
dt.Columns.Add(new DataColumn("Server", typeof(String)));
for (int i = 0; i <= slots - 1; i++)
{
DataRow dr = dt.NewRow();
dr[0] = i + 1;
searchIndex = dv.Find(i + 1);
if (searchIndex != -1)
{
dr[1] = dv[searchIndex][2].ToString();
}
else
{
dr[1] = "-----";
}
dt.Rows.Add(dr);
}
this.GridView1.DataSource = dt;
GridView1.DataBind();
}
catch (Exception)
{
}
ConnectionString = WebConfigurationManager.ConnectionStrings["horizonConnectionString"].ToString();
selectSQL = "SELECT [ServerName], [ServerID], [FarmName], [LMG] FROM [srv] ORDER BY [ServerName]";
con = new SqlConnection(ConnectionString);
cmd = new SqlCommand(selectSQL, con);
DataTable dt3 = new DataTable();
try
{
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(selectSQL, con);
sda.Fill(dt3);
this.GridView2.DataSource = dt3;
GridView2.DataBind();
}
catch (Exception)
{
}
}
you are using the same instance of SqlDataAdapter for two connections at the same time.
SqlConnection con = new SqlConnection(ConnectionString);
SqlCommand cmd = new SqlCommand(selectSQL, con);
the SqlDataAdapter object has been named "sda" twice in "page_load" method.
SqlDataAdapter sda = new SqlDataAdapter(selectSQL, con);
SqlDataAdapter "sda" is already binding to GridView1. this data binding will be lost if U change or re-assign this object.
I've populated a DataTable with a DataAdapter but I would like to change the values dynamically for all of the rows in that column in the DataTable. How do I go about doing this?
Here is my current code:
SqlConnection conn = null;
string sSQL = "";
string connString = "Datasourceetc";
sSQL = "SELECT TEST_ID FROM TEST_TABLE";
SqlDataAdapter sda = new SqlDataAdapter(sSQL, conn);
DataTable dt = new DataTable();
sda.Fill(dt);
foreach (DataRow row in dt.Rows)
{
dt.Columns["TEST_ID"] = "changed"; //Not changing it?!
}
GridView1.DataSource = dt;
GridView1.DataBind();
Thanks
Everything above looks good except when iterating through the rows you can access the columns like so...
foreach (DataRow row in dt.Rows)
{
row["ColumnName"] = "changed"; //Not changing it?!
}
You need to fill first, then change!
SqlConnection conn = new SqlConnection(connString);
string sSQL = "SELECT TEST_ID FROM TEST_TABLE";
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(sSQL, conn);
// fill the DataTable - now you should have rows and columns!
sda.Fill(dt);
// one you've filled your DataTable, you should be able
// to iterate over it and change values
foreach (DataRow row in dt.Rows)
{
row["TEST_ID"] = "changed"; //Not changing it?!
}
GridView1.DataSource = dt;
GridView1.DataBind();
Marc