11-Apr-07 (Created: 11-Apr-07) | More in 'CS-dotnet'

what does databind do for a datagrid?

A simple aspx page with a datagrid


<h2>A sample data grid</h2>

<asp:GridView ID="testGrid" runat="server">
</asp:GridView>

Sample code for binding to this dataset


public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //this.testTable
        this.testGrid.DataSource = getSampleDataset();
        this.testGrid.DataBind();
    }
    private DataSet getSampleDataset()
    {
        DataSet ds = new DataSet();
        string xmlData = "<XmlDS>";
        xmlData+= "<table1><col1>Value1</col1><col2>value</col2></table1>";
        xmlData += "<table1><col1>Value2</col1><col2>value</col2></table1>";
        xmlData += "<table1><col1>Value2</col1><col2>value</col2></table1>";
        xmlData += "<table1><col1>Value2</col1><col2>value</col2></table1>";
        xmlData += "<table1><col1>Value2</col1><col2>value</col2></table1>";
        xmlData+= "</XmlDS>";
        System.IO.StringReader xmlsr = new System.IO.StringReader(xmlData);
        ds.ReadXml(xmlsr, XmlReadMode.InferSchema);
        return ds;
    }
}//eof-class