In This Article I am going to show You How to Bind GridView with Array List using Datatable and multidimensional array.
to bind dropdown
string[,] mularray = {
{ "Dev", "0001","6" },
{ "Raj", "0003" ,"7"},
{ "Tirth", "0004" , "8"},
};
ArrayList arrList = new ArrayList();
for (int i = 0; i < mularray.GetLength(0); i++)
{
arrList.Add(new ListItem(mularray[i, 0], mularray[i, 2]));
}
ddlp.DataSource = arrList;
ddlp.DataTextField = "text";
ddlp.DataValueField = "value";
ddlp.DataBind();
lets have a code to bind Gridview using simple table which will swap in table and bind gridview.
ArrayList MyarrayList = new ArrayList();
DataTable temporarytableDt = new DataTable();
temporarytableDt.Columns.Add("Id");
temporarytableDt.Columns.Add("Name", typeof(String));
temporarytableDt.Columns.Add("Email", typeof(String));
temporarytableDt.Rows.Add(1, "dev", "dev@cxom");
temporarytableDt.Rows.Add(2, "deva", "deva@cxom");
.cs code
for (int i = 0; i < temporarytableDt.Rows.Count; i++)
{
string strbinfval = temporarytableDt.Rows[i]["Name"].ToString();
MyarrayList.Insert(Convert.ToInt32(temporarytableDt.Rows[i]["Id"]) - 1,
temporarytableDt.Rows[i]["Name"].ToString());
}
grdarraylist.DataSource = temporarytableDt;
grdarraylist.DataBind();
.aspx code.
<div>
<asp:GridView runat="server" ID="grdarraylist" AutoGenerateColumns="false" Caption = "Bind Grid With Table"
Width="400px">
<AlternatingRowStyle BackColor ="ActiveBorder" BorderColor="ActiveCaption" VerticalAlign="Middle"
/>
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Email" HeaderText="Email" />
</Columns>
</asp:GridView>
</div>
now i wanted to bind gridview using multidimensional or two dimensional array.
.cs page
string[,] mularray = {
{ "Dev", "0001","6" },
{ "Raj", "0003" ,"7"},
{ "Tirth", "0004" , "8"},
};
ArrayList arrList = new ArrayList();
for (int i = 0; i < mularray.GetLength(0); i++)
{
arrList.Add(new ListItem(mularray[i, 0], mularray[i, 2]));
}
grid.DataSource = arrList;
grid.DataBind();
just fetched data into dimensional array and added in array list.
.aspx page
<asp:GridView runat="server" ID="grid" AutoGenerateColumns="false" Caption = "Bind Grid With ArrayList" Width="400px">
<AlternatingRowStyle BackColor ="ActiveBorder" BorderColor="ActiveCaption" VerticalAlign="Middle"/>
<Columns>
<asp:BoundField DataField="Text" HeaderText="Id" />
<asp:BoundField DataField="Value" HeaderText="Name" />
</Columns>
</asp:GridView>
to bind dropdown
string[,] mularray = {
{ "Dev", "0001","6" },
{ "Raj", "0003" ,"7"},
{ "Tirth", "0004" , "8"},
};
ArrayList arrList = new ArrayList();
for (int i = 0; i < mularray.GetLength(0); i++)
{
arrList.Add(new ListItem(mularray[i, 0], mularray[i, 2]));
}
ddlp.DataSource = arrList;
ddlp.DataTextField = "text";
ddlp.DataValueField = "value";
ddlp.DataBind();
lets have a code to bind Gridview using simple table which will swap in table and bind gridview.
ArrayList MyarrayList = new ArrayList();
DataTable temporarytableDt = new DataTable();
temporarytableDt.Columns.Add("Id");
temporarytableDt.Columns.Add("Name", typeof(String));
temporarytableDt.Columns.Add("Email", typeof(String));
temporarytableDt.Rows.Add(1, "dev", "dev@cxom");
temporarytableDt.Rows.Add(2, "deva", "deva@cxom");
.cs code
for (int i = 0; i < temporarytableDt.Rows.Count; i++)
{
string strbinfval = temporarytableDt.Rows[i]["Name"].ToString();
MyarrayList.Insert(Convert.ToInt32(temporarytableDt.Rows[i]["Id"]) - 1,
temporarytableDt.Rows[i]["Name"].ToString());
}
grdarraylist.DataSource = temporarytableDt;
grdarraylist.DataBind();
.aspx code.
<div>
<asp:GridView runat="server" ID="grdarraylist" AutoGenerateColumns="false" Caption = "Bind Grid With Table"
Width="400px">
<AlternatingRowStyle BackColor ="ActiveBorder" BorderColor="ActiveCaption" VerticalAlign="Middle"
/>
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Email" HeaderText="Email" />
</Columns>
</asp:GridView>
</div>
now i wanted to bind gridview using multidimensional or two dimensional array.
.cs page
string[,] mularray = {
{ "Dev", "0001","6" },
{ "Raj", "0003" ,"7"},
{ "Tirth", "0004" , "8"},
};
ArrayList arrList = new ArrayList();
for (int i = 0; i < mularray.GetLength(0); i++)
{
arrList.Add(new ListItem(mularray[i, 0], mularray[i, 2]));
}
grid.DataSource = arrList;
grid.DataBind();
just fetched data into dimensional array and added in array list.
.aspx page
<asp:GridView runat="server" ID="grid" AutoGenerateColumns="false" Caption = "Bind Grid With ArrayList" Width="400px">
<AlternatingRowStyle BackColor ="ActiveBorder" BorderColor="ActiveCaption" VerticalAlign="Middle"/>
<Columns>
<asp:BoundField DataField="Text" HeaderText="Id" />
<asp:BoundField DataField="Value" HeaderText="Name" />
</Columns>
</asp:GridView>
0 comments: