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();
}
Related
I have a textbox that I'm using to search a customer from my database.
this is my code :
DataView dv = new DataView(tabSearchCustomer);
dv.RowFilter = string.Format("CONVERT({0},System.String) LIKE '%{1}%'", "ID", txtboxSearchCustomer.Text);
dgvCustomers.DataSource = dv;
dgvCustomers.DataSource = dv only shows the new data , it doesn't replace it in the dgv.
I want that my dgv will updated with the new data I was searched (replace the old data in the dgv) , how can I do that ?
My dgv before the search :
My dgv after the search :
public static DataTable ExecuteQuery(string storedProcedure, SqlParameter[] parameters)
{
using (SqlConnection sqlConnection = new SqlConnection(DLLConfig.GetDataBaseConnection()))
{
DataTable dataTable = new DataTable();
SqlCommand sqlCommand = new SqlCommand(storedProcedure, sqlConnection);
sqlCommand.CommandTimeout = 0;
sqlCommand.CommandType = CommandType.StoredProcedure;
foreach (SqlParameter param in parameters)
{
sqlCommand.Parameters.Add(param);
}
SqlDataAdapter sqlAdapter = new SqlDataAdapter(sqlCommand);
sqlAdapter.Fill(dataTable);
SqlConnection.ClearAllPools();
return dataTable;
}
}
Usage:
dataGridView1.DataSource = ExecuteQuery().DefaultView;
I am developing a C# windows application. I am trying to read excel file and to display column name as check box entity. I used the following code but can't display anything in the DataGridView.
using (OleDbCommand cmd = new OleDbCommand())
{
using (OleDbDataAdapter oda = new OleDbDataAdapter())
{
DataTable dt = new DataTable();
cmd.CommandText = "SELECT * From [" + sheetName + "]";
cmd.Connection = con;
con.Open();
oda.SelectCommand = cmd;
oda.Fill(dt);
con.Close();
for (int i = 0; i < dt.Columns.Count;i++)
{
DataColumn dc = dt.Columns[i];
CheckBox Ckb = new CheckBox();
Ckb.Name = dc.ToString();
dataGridView1.DataSource = dt.Columns[i].ToString();
}
}
}
I want the out put to look like the following :
Thanks
Why do you want to populate the DataGrid in a For Loop?
Before the For loop you can try this:
DataSet ds = new DataSet();
Oda.Fill(ds, "Table");
dataGridView1.DataSource = ds.Tables["Table"];
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 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.
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