I have a gridview that populates from a data table when a button is clicked.
The code goes like this:
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] {
new DataColumn("One", typeof(string)),
new DataColumn("Two", typeof(string)),
new DataColumn("Three",typeof(string)) });
string strConnectionString = ConfigurationManager.ConnectionStrings["---"].ConnectionString;
SqlConnection conn = new SqlConnection(strConnectionString);
using (SqlCommand cmd = new SqlCommand("Select * FROM ---", conn))
{
conn.Open();
cmd.ExecuteNonQuery();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
dt.Rows.Add(reader["One"].ToString(), reader["Two"].ToString(), reader["Three"].ToString());
}
conn.Close();
}
GridView2.DataSource = dt;
GridView2.DataBind();
Now whenever i try to add a DataKeyName to the gridview, it would not be populated.
I tried setting it manually and using
GridView2.DataKeyNames = new string[] {"Key"};
Related
I'm trying to populate a datagridview based on the selected row in another, as an object kind of.
public Form1()
{
dg1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
Allemployees(dg1);
}
The following function is returning all employees to the first dg
private void Allemployees(DataGridView dg)
{
conn.Open();
SqlCommand cmd = new SqlCommand("select * from emp", conn);
SqlDataReader reader = cmd.ExecuteReader();
DataTable table = new DataTable();
table.Load(reader);
dg.DataSource = table;
conn.Close();
}
How do i use selected row as an object and pass it to return emp_loc relations
private void locsbyemp(DataGridView dg, int emp_id)
{
conn.Open();
SqlCommand cmd = new SqlCommand(#"select loc_name From emp_locs WHERE emp_id = #emp", conn);
cmd.Parameters.AddWithValue("#emp", emp_id);
SqlDataReader reader = cmd.ExecuteReader();
DataTable table = new DataTable();
table.Load(reader);
dg.DataSource = table;
conn.Close();
}
same thing with trying to populate "edit emp form" controls with selected row data
Thanks in advance
I have dropdownlist filled by database. how can I make it searchable?
I can do it with some issues but when data bind with database in note working.
is my filling method wrong?
<asp:Label ID="lbProbType" runat="server" Text="Problem Type""
Font-Bold="True"></asp:Label>
if (!IsPostBack)
{
SqlConnection cn = new SqlConnection(#"");
string readnamesquery = "select cwFullTitle from tbCowWorkers order by cwLName";
cn.Open();
SqlCommand cmd = new SqlCommand(readnamesquery, cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
dpCaller.DataSource = dt;
dpCaller.DataTextField = "cwFullTitle";
dpCaller.DataBind();
cn.Close();
}
I am working on windows application,combo box works perfectly but by default when I run the page ,it shows blank , I want to show the default value.I am using this code.
public void combofill()
{
cmb.Items.Clear();
cmb.Items.Add("select");
SqlConnection con = new SqlConnection(con1);
con.Open();
SqlCommand cmd = new SqlCommand("Select Name from mr000 where Type&0x0f=3", con);
SqlDataReader dr;
dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
cmb.DataSource = dt;
cmb.Name = "combocust";
cmb.DisplayMember = "Name";
cmb.HeaderText = "Change Customer";
dataGridView1.Columns.Insert(3, cmb);
con.Close();
}
try this code
public void FillCmbo(String Sql, ComboBox cmb, String name)
{
try
{
if (Con.State == ConnectionState.Open)
Con.Close();
Con.Open();
SqlConnection con = new SqlConnection(Sql, Con);
SqlCommand cmd = new SqlCommand("Select Name from mr000 where Type&0x0f=3", con);
DataTable dt = new DataTable();
con.Fill(dr);
dt.Rows.InsertAt(Drw, 0);
cmb.Name = "combocust";
cmb.DisplayMember =name;
cmb.HeaderText = "Change Customer";
cmb.DataSource = dt;
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message, "Check", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
you can also use
ComboBox.selectIndex=0;
I use OleDbCommand to run the sqlcommand that can update the Access database.
But when I try to use OleDbDataAdapter and DataTable to update the database,
it doesn't work.
using (OleDbConnection conn = new OleDbConnection(connStr))
{
conn.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter("select * from confirm", conn);
OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);
DataTable table = new DataTable();
adapter.Fill(table);
DataRow row = table.NewRow();
row["k"] = "november";
row["v"] = "eleven";
// table.AcceptChanges();
adapter.UpdateCommand = builder.GetUpdateCommand();
adapter.Update(table);
// table.AcceptChanges();
return table;
}
When I run the code, the database doesn't change.
This DataRow row = table.NewRow(); creates a row with table shema but doesn't add the row to the DataTable. You need to add the row to the table :
table.Rows.Add(row);
complete code
using (OleDbConnection conn = new OleDbConnection(connStr))
{
conn.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter("select * from confirm", conn);
OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);
DataTable table = new DataTable();
adapter.Fill(table);
DataRow row = table.NewRow();
row("k") = "november";
row("v") = "eleven";
//**You missed this**
table.Rows.Add(row);
adapter.UpdateCommand = builder.GetUpdateCommand();
adapter.Update(table);
// table.AcceptChanges();
return table;
}
How To Do This Work on gridviewcomboboxcolumns any idea plx
//Form Load Event
string query="select article_name from article";
SqlCommmand cmd = new SqlCommand(query,con);
SqlDataAdapter da= new SqlDataAdapter(cmd);
DataTable dt=new DataTable();
da.Fill(dt);
combobox1.items.clear();
for(int i=0;i<dt.rows.count;i++)
{
combobox1.items.add(dt.rows[i].cells[0].toString());
}
\ComboBox1 Selected IndexChange Event
string query1="select description from article where article_name='"+combobox1.selectedItem.ToString()+"'";
SqlCommmand cmd1 = new SqlCommand(query1,con);
SqlDataAdapter da1= new SqlDataAdapter(cmd);
DataTable dt1=new DataTable();
da1.Fill(dt1);
combobox2.items.clear();
for(int i=0;i<dt1.rows.count;i++)
{
combobox2.items.add(dt1.rows[i].cells[0].toString());
}
\Now Assume these 2 combox is gridviewCombobox Columns so how to make
this work on gridviewcombobox columns
Project in Windows Form in C#
I m posting this answer after a few months because its helps for
thoose whoose facing problem on DataGridviewComboboxcell
I did my own skill First Fill my first/Main Column
SqlCommand objCmd = new SqlCommand("select distinct article_name from Setup_article_custominvoice", con);
SqlDataAdapter objDA = new SqlDataAdapter(objCmd);
objDA.SelectCommand.CommandText = objCmd.CommandText.ToString();
DataTable dt = new DataTable();
objDA.Fill(dt);
article.DataSource = dt;
//this column1 will display as text
article.DisplayMember = "article_name";
After that i was going on Cell End Edit
if (dataGridView1.CurrentCell == dataGridView1.CurrentRow.Cells["article_name"])
{
string CategoryValue = "";
//string CategoryValue1 = "";
if (dataGridView1.CurrentCell.Value != null)
{
CategoryValue = dataGridView1.CurrentCell.Value.ToString();
//CategoryValue1 = dataGridView1.CurrentCell.Value.ToString();
}
//SqlConnection objCon = new SqlConnection(#"Data Source=.\SqlExpress;Initial Catalog=dbTest3;Integrated Security=True");
string query = "select article_name,composition from Setup_article_custominvoice where article_name='" + CategoryValue + "'";
SqlCommand objCmd = new SqlCommand(query, con);
SqlDataAdapter objDA = new SqlDataAdapter(objCmd);
objDA.SelectCommand.CommandText = objCmd.CommandText.ToString();
DataTable dt = new DataTable();
objDA.Fill(dt);
DataGridViewComboBoxCell t = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[2] as DataGridViewComboBoxCell;
t.DataSource = dt;
t.DisplayMember = "composition";
}