Trouble With Displaying Data in DataGridView - c#

I am trying to display my mysql database table on a DataGridView using c# . but nothings happening . please tell me where i've gone wrong in my code
private void button1_Click(object sender, EventArgs e)
{
MySqlConnection conn = new MySqlConnection("Server = localhost; Database=project; user=root;password=''");
conn.Open();
string query = "SELECT * FROM billing";
MySqlDataAdapter bills = new MySqlDataAdapter(query, conn);
MySqlCommandBuilder cbuilder = new MySqlCommandBuilder(bills);
DataTable dtable = new DataTable();
bills.Fill(dtable);
//the DataGridView
DataGridView dgView = new DataGridView();
//BindingSource to sync DataTable and DataGridView
BindingSource bSource = new BindingSource();
//set the BindingSource DataSource
bSource.DataSource = dtable;
//set the DataGridView DataSource
dgView.DataSource = bSource;
bills.Update(dtable);
}

Related

How to associate mysql data to DataRepeater controls

I have a DataRepeater template control with 2 textboxes which I want to associate data from a mysql table
When I run this program I get the representation on my DataRepeater the number of rows existing in my mysql table
RESULT
my question is how to associate the data to those textboxes
private void Form1_Load(object sender, EventArgs e)
{
BindLviewData();
}
protected void BindLviewData()
{
System.Data.DataTable dt = new System.Data.DataTable("db.myTable");
MySqlConnection dbConn = new MySqlConnection("Server = localhost; Database = db; Uid = wwww; Pwd = www; ");
dbConn.Open();
string query = string.Format("SELECT Col1,Col2 FROM myTable");
MySqlCommand cmd = new MySqlCommand(query, dbConn);
MySqlDataAdapter returnVal = new MySqlDataAdapter(cmd);
returnVal.Fill(dt);
BindingSource bs = new BindingSource();
bs.DataSource = dt;
dataRepeater1.DataSource = bs;
dbConn.Close();
}

Show MySQL database values in DataGridView C#

I made a connection to my database, but when I try to view some data from 1 table, is not showing in dataGridView. When I am pressing the button, few seconds seems like is going to show the values, and after that nothing posted in dataGridView
Can anyone explain if I made sth wrong in my code?
I am working in Visual Studio 2013
My code:
private void button1_Click(object sender, EventArgs e)
{
string connectionString = "datasource=localhost;database=microweb;port=3306;username=root;password=pass";
MySqlConnection conn = new MySqlConnection(connectionString);
MySqlCommand cmd = new MySqlCommand("select * from reservations", conn);
try
{
MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = cmd;
DataTable dta = new DataTable();
sda.Fill(dta);
BindingSource bdsour = new BindingSource();
bdsour.DataSource = dta;
dataGridView1.DataSource = bdsour;
sda.Update(dta);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
You should set AutoGenerateColumns to true
bdsour.DataSource = dta;
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = bdsour;

combobox /dropdownlist in grid view c#.net

I am a newbie to c#.net winforms application. I have a data grid wch displays some data and out of which one of the column shud be made a dropdownlist or combobox. how do I use that in my code. please help.
private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
DataGridViewComboBoxColumn combo = (DataGridViewComboBoxColumn)dataGridView1.Rows[e.RowIndex].Cells[3].OwningColumn;
sql = "select NAME FROM Suppliers";
BindingSource bsource = new BindingSource();
bsource.DataSource = obj.SqlDataTable(sql);
dataGridView1.DataSource = bsource;
combo.HeaderText = "Select Supplier";
}
I want to populate the 3rd column of the data grid with supplier name from the corresponding suppliers table.The data grid view is already populated with data from a join query and one of the field is Supplier(wch I mwant to convert to a dropdown or combo box). let me know if you need any further info for clarifications.
I Modified my code as follows:
DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
combo.HeaderText = "Suppliers";
//execute sql data adapter to get supplier values
DataTable dt = obj.SqlDataTable("select NAME from CUSTOMERS");
foreach (DataRow supplier in dt.DataSet.Tables[0].Rows)
{
combo.Items.Add(supplier[0]);
}
dataGridView1.Columns.Add(combo);
now am getting a null reference exception at " dt.DataSet.Tables[0].Rows"
. plz help. I am not sure if am missing something.
Try this code
string strcon = #"Data Source=kp;Initial Catalog=Name;Integrated Security=True;Pooling=False";
SqlConnection con;
SqlCommand cmd;
SqlDataAdapter da;
DataTable dt;
DataGridViewComboBoxColumn dgvCmb;
public Form2()
{
InitializeComponent();
grdcmd();
}
public void grdcmd()
{
con = new SqlConnection(strcon);
con.Open();
string qry = "Select * from Dbname";
da = new SqlDataAdapter(qry, strcon);
dt = new DataTable();
da.Fill(dt);
dgvCmb = new DataGridViewComboBoxColumn();
foreach (DataRow row in dt.Rows)
{
dgvCmb.Items.Add(row["Fname"].ToString());
}
dataGridView1.Columns.Add(dgvCmb);
}

How to refresh DataGridView?

I am trying to force DataGrid to refresh its content on a form closing event. I have tried various methods, searched this forum for answers but still I am unable to find a solution.
This is how my DataGridView is being populated :
string strCon = ConfigurationManager.ConnectionStrings["ST"].ConnectionString.ToString();
string strSQL = "SQL QUERY";
SqlDataAdapter dataAdapter = new SqlDataAdapter(strSQL, strCon);
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
bindingSource1.DataSource = table;
dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
dataGridView1.ReadOnly = true;
dataGridView1.DataSource = bindingSource1;
I have tried the Invalidate method, BindingSource.ResetBindings but still the GridView doesn't refresh. How do I force refresh it?
Data refresh
I think you are talking about data refresh. In this case you can just rebind your DatagridView to the source:
DataGridView dataGridView1 = new DataGridView();
dataGridView1.DataSource = aDataSource;
//Update Data in aDataSource
//...
dataGridView1 = null;
dataGridView1.DataSource = aDataSource;
In your code snapshot aDataSource should actually be bindingSource1. As #GIVE-ME-CHICKEN mentioned, in your case you should enclose the re-binding inside the proper callback to attach it to the right event:
private void Form1_FormClosing(object sender, FormClosingEventArgs e){ ... }
Data refresh
If you don't want to rebind your source you can adopt another solution: binding your dataGridView1 to a Collection Observable. In this case, it will be enough to implement the INotifyCollectionChanged interface to notify the change to its listeners (your DataGridView component)
Graphic refresh
If you are searching only for a graphic refresh, there is a specific method:
dataGridView1.Refresh();
Can you try and change this line:
bindingSource1.DataSource = table;
to
bindingSource1.DataSource = table.DefaultView;
Maybe doing it similar to this way helps?
public DataTable GetData(string SQLQuery)
{
string strCon = ConfigurationManager.ConnectionStrings["ST"].ConnectionString.ToString();
string strSQL = "SQL QUERY";
SqlDataAdapter dataAdapter = new SqlDataAdapter(strSQL, strCon);
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
//Maybe only one of the clear code works.
dataGridView1.Rows.Clear();
dataGridView1.DataSource = null;
BindingSource bindingSource1 = new BindingSource();
bindingSource1.DataSource = GetData("SQL QUERY");
dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
dataGridView1.ReadOnly = true; //<-----------Try taking this off aswell
dataGridView1.DataSource = bindingSource1;
}

Grid view is not appearing when im running my website

So i have a website hosted on blacknight.com. Im coding with cSharp and asp.net I have mySQL database stored within blacknight also.
However I want to add an admin section to my site where a user could log in and press a "load data" button and the registration table values from blacknight database would appear in a grid view.
However its just not working. Im wondering do I have to physically connect my gridview to my database? Because I have tried to connect to my hosted database with my gridview and it keeps saying it cant connect.
Below is the code behind my load data button. As as it stands when I upload my adminpages to blacknight and run, the gridview is not even appearing. This is my Fourth year project for college and I really need to get it working. Any help would be much appreciative.
protected void Button1_Click(object sender, EventArgs e)
{
string constring ="Server=xxxx; Database=xxxx; Uid=xxx; Pwd=xxx";
MySqlConnection conDb1317466_bk = new MySqlConnection(constring);
DataSet dbdataset = new DataSet();
//binding.DataSource = this.bindingSource.DataSource;
MySqlCommand cmdDb1317466_bk = new MySqlCommand("Select * from db1317466_bk.registration;", conDb1317466_bk);
try
{
MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = cmdDb1317466_bk;
DataTable dbdataset1 = new DataTable();
sda.Fill(dbdataset);
BindingSource bSource = new BindingSource();
bSource.DataSource = dbdataset;
//DataGridView1.DataSource = bSource;
DataGridView1.DataBind();
sda.Update(dbdataset);
}
catch (Exception)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "scriptkey", "<script>alert('Can Load data');</script>");
}
}
}
Try this one
GridView1.DataSource=dbdataset;
GridView1.DataBind();
Instead of using
BindingSource bSource = new BindingSource();
bSource.DataSource = dbdataset;
DataGridView1.DataBind();
Also, I don't think that you should use:
sda.Update(dbdataset);
Try this
try
{
MySqlDataAdapter da = new MySqlDataAdapter();
da.SelectCommand = cmdDb1317466_bk;
DataTable dt= new DataTable();
da.Fill(dt);
DataGridView1.DataSource = dt;
DataGridView1.DataBind();
}

Categories