Presentation is loading. Please wait.

Presentation is loading. Please wait.

DataGridView. Displaying data in a tabular format is a task you are likely to perform frequently. The DataGridView control is designed to be a complete.

Similar presentations


Presentation on theme: "DataGridView. Displaying data in a tabular format is a task you are likely to perform frequently. The DataGridView control is designed to be a complete."— Presentation transcript:

1 DataGridView

2 Displaying data in a tabular format is a task you are likely to perform frequently. The DataGridView control is designed to be a complete solution for displaying tabular data with Windows Forms. The DataGridView control is highly configurable and extensible, and it provides many properties, methods, and events to customize its appearance and behavior. The DataGridView control makes it easy to define the basic appearance of cells and the display formatting of cell values. The cell is the fundamental unit of interaction for the DataGridView. All cells derive from the DataGridViewCell base class. Each cell within the DataGridView control can have its own style, such as text format, background color, foreground color, and font. Typically, however, multiple cells will share particular style characteristics. The data type for the cell's Value property by default is of type Object.

3 The DataGridView can display data in Bound mode, unbound mode and Virtual mode.  Bound mode is suitable for managing data using automatic interaction with the data store. One very common use of the DataGridView control is binding to a table in a database.  Unbound mode is suitable for displaying relatively small amounts of data that you manage programmatically.  Virtual mode gives you a higher degree of control by allowing you to wait until a cell is actually being displayed to provide the value it will contain. C# DataGridView Binding - SQL Server dataset

4 using System; using System.Data; using System.Windows.Forms; using System.Data.SqlClient; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string connectionString = "Data Source=.;Initial Catalog=pubs;Integrated Security=True"; string sql = "SELECT * FROM Authors"; SqlConnection connection = new SqlConnection(connectionString); SqlDataAdapter dataadapter = new SqlDataAdapter(sql, connection); DataSet ds = new DataSet(); connection.Open(); dataadapter.Fill(ds, "Authors"); connection.Close(); dataGridView1.DataSource = ds; dataGridView1.DataMember = "Authors_table); } C# DataGridView Binding - SQL Server dataset

5 The easiest way to get started using the DataGridView control is to use it in basic data-binding scenarios. When you specify a data source for the DataGridView, by default it will construct columns for you automatically. This will be created based on the data types in the data source. DataGridView Binding - OLEDB dataset

6 using System; using System.Data; using System.Windows.Forms; using System.Data.OleDb; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=your.mdb file;"; string sql = "SELECT * FROM Authors_table"; OleDbConnection connection = new OleDbConnection(connetionString); OleDbDataAdapter dataadapter = new OleDbDataAdapter(sql, connection); DataSet ds = new DataSet(); connection.Open(); dataadapter.Fill(ds, "Authors_table"); connection.Close(); dataGridView1.DataSource = ds; dataGridView1.DataMember = "Authors_table"; } }} DataGridView Binding - OLEDB dataset

7 C# DataGridView Add Columns and Rows The DataGridView control is designed to be a complete solution for displaying tabular data with Windows Forms. The DataGridView control is highly configurable and extensible, and it provides many properties, methods, and events to customize its appearance and behavior. All cells in a DataGridView control derive from the DataGridViewCell base class. You can access the DataGridView control's columns by using the Columns collection and DataGridView control's rows by using the Rows collection. The below code shows how to manually create Columns and Rows in a DataGridView. dataGridView1.Columns[Index].Name = "Column Name";

8 C# DataGridView Add Columns and Rows using System; using System.Data; using System.Windows.Forms; using System.Data.SqlClient; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { dataGridView1.ColumnCount = 3; dataGridView1.Columns[0].Name = "Product ID"; dataGridView1.Columns[1].Name = "Product Name"; dataGridView1.Columns[2].Name = "Product Price"; string[] row = new string[] { "1", "Product 1", "1000" }; dataGridView1.Rows.Add(row); row = new string[] { "2", "Product 2", "2000" }; dataGridView1.Rows.Add(row); row = new string[] { "3", "Product 3", "3000" }; dataGridView1.Rows.Add(row); row = new string[] { "4", "Product 4", "4000" }; dataGridView1.Rows.Add(row); }

9 C# DataGridView Sorting and Filtering The DataGridView control is highly configurable and extensible, and it provides many properties, methods, and events to customize its appearance and behavior. A DataView provides a means to filter and sort data within a DataTable. The below code shows how to filter and sort a DataGridView by using a DataView. dv = new DataView(ds.Tables[0], "Price > 19", "Price Desc", DataViewRowState.CurrentRows); dataGridView1.DataSource = dv;

10 C# DataGridView Sorting and Filtering using System; using System.Data; using System.Windows.Forms; using System.Data.SqlClient; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string connectionString = "Data Source=.;Initial Catalog=pubs;Integrated Security=True"; string sql = "SELECT * FROM Titles"; SqlConnection connection = new SqlConnection(connectionString); SqlDataAdapter dataadapter = new SqlDataAdapter(sql, connection); DataSet ds = new DataSet(); connection.Open(); dataadapter.Fill(ds, "Titles_table"); connection.Close(); DataView dv; dv = new DataView(ds.Tables[0], "Price > 19", "Price Desc", DataViewRowState.CurrentRows); dataGridView1.DataSource = dv; } }}

11 Add Button to C# DataGridView The DataGridView control and its related classes are designed to be a flexible, extensible system for displaying and editing tabular data.The DataGridView control is highly configurable and extensible, and it provides many properties, methods, and events to customize its appearance and behavior. The DataGridView control provides TextBox, CheckBox, Image, Button, ComboBox and Link columns with the corresponding cell types. Program on next slide shows how to add a Button in Cell of a DataGridView control. Also it showing in the dataGridView1_CellClick event which button the user clicked.

12 Add Button to C# DataGridView using System; using System.Data; using System.Windows.Forms; using System.Data.SqlClient; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { dataGridView1.ColumnCount = 3; dataGridView1.Columns[0].Name = "Product ID"; dataGridView1.Columns[1].Name = "Product Name"; dataGridView1.Columns[2].Name = "Product Price"; string[] row = new string[] { "1", "Product 1", "1000" }; dataGridView1.Rows.Add(row); row = new string[] { "2", "Product 2", "2000" }; dataGridView1.Rows.Add(row); row = new string[] { "3", "Product 3", "3000" }; dataGridView1.Rows.Add(row); row = new string[] { "4", "Product 4", "4000" }; dataGridView1.Rows.Add(row); DataGridViewButtonColumn btn = new DataGridViewButtonColumn(); dataGridView1.Columns.Add(btn); btn.HeaderText = "Click Data"; btn.Text = "Click Here"; btn.Name = "btn"; btn.UseColumnTextForButtonValue = true; } private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex == 3) { MessageBox.Show((e.RowIndex+1) + " Row " + (e.ColumnIndex+1) + " Column button clicked "); }

13 C# DataGridView Database Operations using System; using System.Data; using System.Windows.Forms; using System.Data.SqlClient; namespace WindowsFormsApplication1 { public partial class Form1 : Form { SqlCommand sCommand; SqlDataAdapter sAdapter; SqlCommandBuilder sBuilder; DataSet sDs; DataTable sTable; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string connectionString = "Data Source=.;Initial Catalog=pubs;Integrated Security=True"; string sql = "SELECT * FROM Stores"; SqlConnection connection = new SqlConnection(connectionString); connection.Open(); sCommand = new SqlCommand(sql, connection); sAdapter = new SqlDataAdapter(sCommand); sBuilder = new SqlCommandBuilder(sAdapter); sDs = new DataSet(); sAdapter.Fill(sDs, "Stores"); sTable = sDs.Tables["Stores"]; connection.Close(); dataGridView1.DataSource = sDs.Tables["Stores"]; dataGridView1.ReadOnly = true; save_btn.Enabled = false; dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; } private void new_btn_Click(object sender, EventArgs e) { dataGridView1.ReadOnly = false; save_btn.Enabled = true; new_btn.Enabled = false; delete_btn.Enabled = false; } private void delete_btn_Click(object sender, EventArgs e) { if (MessageBox.Show("Do you want to delete this row ?", "Delete", MessageBoxButtons.YesNo) == DialogResult.Yes) { dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index); sAdapter.Update(sTable); } private void save_btn_Click(object sender, EventArgs e) { sAdapter.Update(sTable); dataGridView1.ReadOnly = true; save_btn.Enabled = false; new_btn.Enabled = true; delete_btn.Enabled = true; }

14 VScrollBar v = new VScrollBar(); v.Height = 300; v.Width = 23; v.Maximum = 100; v.Minimum = 20; v.Value = 45; this.Controls.Add(v); v.Scroll += new ScrollEventHandler(v_Scroll); private void v_Scroll(object sender, ScrollEventArgs e) { Graphics g = pictureBox1.CreateGraphics(); g.DrawImage(pictureBox1.Image, new Rectangle(0, 0, pictureBox1.Height, v.Value)); } Usage of VScrollbar

15 int _selectedIndex; string _text; private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { // Called when a new index is selected. _selectedIndex = comboBox1.SelectedIndex; _text = comboBox1.Text; Display(); } void Display() { this.Text = string.Format("Text: {0}; SelectedIndex: {1}", _text, _selectedIndex); } Usage of Combobox

16 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { comboBox2.Items.Clear(); if (comboBox1.SelectedItem.ToString() == "1") { comboBox2.Items.Add("Hello"); comboBox2.Items.Add("World"); } else { comboBox2.Items.Add("Test1"); comboBox2.Items.Add("Test2"); } comboBox1.items.clear Usage of Combobox to add item

17 private void Form1_Load(object sender, EventArgs e) { DataTable dt = new DataTable(); using (SqlConnection Cn = new SqlConnection(Properties.Settings.Default.con)) { using (SqlCommand Cmd = new SqlCommand("SELECT * FROM Orders where oid = @oid", Cn)) { Cn.Open(); SqlDataAdapter adpt = new SqlDataAdapter(Cmd); adpt.Fill(dt); comboBox1.DataSource = dt; comboBox1.DisplayMember="Orders"; comboBox1.ValueMember="id"; } Usage of Combobox with database

18 dataset ds = new dataset() ; string querry = "select studentid,studentname from tbstudent"; sqlcommand cmd = new sqlcommand(querry,connection); dataadapter da = new sqldataadapter(cmd); combobox1.valuemember = "studentid"; combobox1.displaymember = "studentname"; combobox1.datasource = ds.tables(0); Another example: string movies = ""; if (checkBox1.Checked) { movies = movies + checkBox1.Text; } MessageBox.Show(movies); or string ChosenMovie = ""; if (radioButton1.Checked) { ChosenMovie = radioButton1.Text; } else if (radioButton2.Checked) { ChosenMovie = radioButton2.Text; }

19 RichTextBox richTextBox1.SaveFile(“F:\\samples\\ …”); richTextbox1.LoadFile(“ ”) richTextBox1.SelectionColor=Color.Green;


Download ppt "DataGridView. Displaying data in a tabular format is a task you are likely to perform frequently. The DataGridView control is designed to be a complete."

Similar presentations


Ads by Google