Saturday, July 12, 2008

DATA GRID, DATA LIST, DATA REPEATER

What is datagrid?

The DataGrid Web server control is a powerful tool for displaying information from a data source. It is easy to use; you can display editable data in a professional-looking grid by setting only a few properties. At the same time, the grid has a sophisticated object model that provides you with great flexibility in how you display the data.

What’s the difference between the System.Web.UI.WebControls.DataGrid and and System.Windows.Forms.DataGrid?

The Web UI control does not inherently support master-detail data structures. As with other Web server controls, it does not support two-way data binding. If you want to update data, you must write code to do this yourself. You can only edit one row at a time. It does not inherently support sorting, although it raises events you can handle in order to sort the grid contents. You can bind the Web Forms DataGrid to any object that supports the IEnumerable interface. The Web Forms DataGrid control supports paging. It is easy to customize the appearance and layout of the Web Forms DataGrid control as compared to the Windows Forms one.


How do you customize the column content inside the datagrid?

If you want to customize the content of a column, make the column a template column. Template columns work like item templates in the DataList or Repeater control, except that you are defining the layout of a column rather than a row.

How do you apply specific formatting to the data inside the cells?

You cannot specify formatting for columns generated when the grid’s AutoGenerateColumns property is set to true, only for bound or template columns. To format, set the column’s DataFormatString property to a string-formatting expression suitable for the data type of the data you are formatting.

How do you hide the columns?

One way to have columns appear dynamically is to create them at design time, and then to hide or show them as needed. You can do this by setting a column’s Visible property.

How do you display an editable drop-down list?

Displaying a drop-down list requires a template column in the grid. Typically, the ItemTemplate contains a control such as a data-bound Label control to show the current value of a field in the record. You then add a drop-down list to the EditItemTemplate. In Visual Studio, you can add a template column in the Property builder for the grid, and then use standard template editing to remove the default TextBox control from the EditItemTemplate and drag a DropDownList control into it instead. Alternatively, you can add the template column in HTML view. After you have created the template column with the drop-down list in it, there are two tasks. The first is to populate the list. The second is to preselect the appropriate item in the list — for example, if a book’s genre is set to “fiction,” when the drop-down list displays, you often want “fiction” to be preselected.

How do you check whether the row data has been changed?

The definitive way to determine whether a row has been dirtied is to handle the changed event for the controls in a row. For example, if your grid row contains a TextBox control, you can respond to the control’s TextChanged event. Similarly, for check boxes, you can respond to a CheckedChanged event. In the handler for these events, you maintain a list of the rows to be updated. Generally, the best strategy is to track the primary keys of the affected rows. For example, you can maintain an ArrayList object that contains the primary keys of the rows to update.

What tags do you need to add within the asp:datagrid tags to bind columns manually?



What tag do you use to add a hyperlink column to the DataGrid?



What property must you set, and what method must you call in your code, in order to bind the data from some data source to the Repeater control?

What property do you have to set to tell the grid which page to go to when using ?

AllowPaging="True"

How can you provide an alternating color scheme in a Repeater control?



Explain what a diffgram is, and a good use for one?

Which template must you provide, in order to display data in a Repeater control?

.. Item formats ..

Differences between Datagrid, Datalist and Repeater?

1. Datagrid has paging while Datalist doesnt.
2. Datalist has a property called repeat. Direction = vertical/horizontal. (This is of great help in designing layouts). This is not there in...

How can I make my first application with DataGrid using the data from ADO.Net?

Let’s create a simple application first that loads a table data from database server to the data grid control. First of all, add a data grid control and a button to your form from Visual Studio toolbox. We have set the Name property of data grid to ‘dgDetails’ and its CaptionText property to ‘ProgrammersHeaven Database’. The name of button is ‘btnLoadData’. The event handler for button is:

C# Version
private void btnLoadData_Click(object sender, System.EventArgs e)
{
string connectionString = "server=FARAZ; database=programmersheaven;" +
"uid=sa; pwd=;";
SqlConnection conn = new SqlConnection(connectionString);
string cmdString = "SELECT * FROM article";
SqlDataAdapter dataAdapter = new SqlDataAdapter(cmdString, conn);
DataSet ds = new DataSet();
dataAdapter.Fill(ds, "article");

dgDetails.SetDataBinding(ds, "article");
}

VB.Net Version
Private Sub btnLoadData_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnLoadData.Click

Dim connectionString As String = "server=P-III; database=programmersheaven;" + _
"uid=sa; pwd=;"
Dim conn As New SqlConnection(connectionString)
Dim cmdString As String = "SELECT * FROM article"
Dim dataAdapter As New SqlDataAdapter(cmdString, conn)
Dim ds As New DataSet()
dataAdapter.Fill(ds, "article")

dgDetails.SetDataBinding(ds, "article")

End Sub

Here we first created data adapter and filled the data set using it as we used to do in other applications. The only new thing is the binding of “article” table to the data grid control which is done by calling the SetDataBinding() method of the DataGrid class. The first parameter of this method is the dataset while the second parameter is the name of table in the dataset.

C# Version
dgDetails.SetDataBinding(ds, "article");


VB.Net Version
dgDetails.SetDataBinding(ds, "article")

When you execute this program and select the Load button you will see the output presented in the previous figure.


How can I make my data grid to view data from multiple related tables?

Let’s see how we can use Data Grid control to show multiple related tables. When two tables are related, one is called the parent table while the other is called the child table. The child table contains the primary key of parent table as a foreign key. For example in our ProgrammersHeaven database, table Author is the parent table of the Article table as the Article table contains ‘AuthorId’ as foreign key which is a primary key in the Author table.


C# Version
private void btnLoadData_Click(object sender, System.EventArgs e)
{
string connectionString = "server=P-III; database=programmersheaven;" +
"uid=sa; pwd=;";
SqlConnection conn = new SqlConnection(connectionString);

string cmdString = "SELECT * FROM article";
SqlDataAdapter dataAdapter = new SqlDataAdapter(cmdString, conn);
DataSet ds = new DataSet();
dataAdapter.Fill(ds, "article");

cmdString = "SELECT * FROM author";
dataAdapter = new SqlDataAdapter(cmdString, conn);
dataAdapter.Fill(ds, "author");

DataRelation relation = new DataRelation("ArtAuth",
ds.Tables["author"].Columns["authorId"],
ds.Tables["article"].Columns["authorId"]
);
ds.Relations.Add(relation);

DataView dv = new DataView(ds.Tables["author"]);
dgDetails.DataSource = dv;
}


VB.Net Version
Private Sub btnLoadData_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnLoadData.Click

Dim connectionString As String = "server=P-III; database=programmersheaven;" + _
"uid=sa; pwd=;"
Dim conn As New SqlConnection(connectionString)
Dim cmdString As String = "SELECT * FROM article"
Dim dataAdapter As New SqlDataAdapter(cmdString, conn)
Dim ds As New DataSet()
dataAdapter.Fill(ds, "article")

cmdString = "SELECT * FROM author"
dataAdapter = New SqlDataAdapter(cmdString, conn)
dataAdapter.Fill(ds, "author")

Dim relation As New DataRelation("ArtAuth", _
ds.Tables("author").Columns("authorId"), _
ds.Tables("article").Columns("authorId") _
)
ds.Relations.Add(relation)


Dim dv As New DataView(ds.Tables("author"))
dgDetails.DataSource = dv
End Sub

Can you edit data in the Repeater control?

No

No comments: