Saturday, March 1, 2014

Datagrid in wpf + custom columns

In Linq

var query = from table in database.tablename
select table;

datagridName.itemsource = query

This Should give all the fields in the database

If you want to show all the tadabase field. this should do it. (Make sure AutoGenerateColumns = "ture" in xaml )

Customize what u want to show in the datagrid

In XAML

<DataGrid x:Name="datagridName"  AutoGenerateColumns="False">
                <DataGrid.Columns>
                    <DataGridTextColumn Width="100" Header="First Beautyful Name" Binding="{Binding Name}" />
                </DataGrid.Columns>
 </DataGrid>


//Note the AutoGenerateColumns = "False" in the DataGrid, This is going to allow us not to show all the filled in the database

// Binding in the DatagridTextColumn = "{Binding Name}" Name is the column name that in my table

C# How to Connect with MySQL Database

Download MySQL Database (I use wamp in localhost)
Download MySQL Connector From  Here, or only the class library (i upload) Here
(MySQL Connector is a class Library, A ".dll" File that implemented for connected to the mysql database

Add mysql.data.dll to the reference,










//variables:
//Server Definitions
        private string server = "localhost";
        private string username = "root";
        private string password = "";
        private string port = "3306";
        private string database = "tallernetsangeetha";

        private string query = null;
         
        //MySql Stuff -- Some May Unused but these are the all
        MySqlConnection con = null;
        MySqlCommand com = null;
        MySqlDataReader read = null;
        MySqlDataAdapter adapter = null;
        DataTable table = null ;


//Declare Connection String        
string conString = "Server=" + server + ";Port=" + port + ";Database=" + database + ";Uid=" + username + ";Password=" + password;

//Connect to the database
con = new MySqlConnection(conString);

//query (estimate is the table name )
query = "Select * From estimates";

com = new MySqlCommand(query, con);

//read got All the value in the table, that you specified in the query            
 read = com.ExecuteReader();

// You can now output the data like
Messagebox.Show(read["ColumnName in the table"])

Simple is that, Next how to use linq with mysql