问题描述
我试图从XML中获取一些数据,然后尝试将该数据分配给DataTable
,然后通过DataGrid
显示.
代码:-
Hi,
I am trying to get some data from a XML and then trying to assign that data to a DataTable
and then displaying it through DataGrid
.
Code:-
// Response.Clear();
// create here table and columns that will be used
// for storing items from the xml file for later binding into grid
DataTable dtable = new DataTable();
dtable.Columns.Add(new DataColumn("Title"));
dtable.Columns.Add(new DataColumn("Description"));
// fetch webrequest. Here, give the path of the location where rss feed is stored.
WebRequest WebReq = WebRequest.Create(@"K:\XYZ.COM\EasyReniewed.Web\rss.xml");
//get webresponse from the webrequset
WebResponse webRes = WebReq.GetResponse();
//use stream to stremline the input from from webresponse.
Stream rssStream = webRes.GetResponseStream();
// Create new xml document and load a XML Document
// with the strem.
XmlDocument xmlDoc = new XmlDocument();
//loads the url from the stream
xmlDoc.Load(rssStream);
// use XmlNodeList to get the matching xmlnodes from the xmldocument
XmlNodeList xmlNodeList = xmlDoc.SelectNodes("rss/channel/item");
//create array of the object for creating the row
object[] RowValues = {"",""};
//Make a Loop through RSS Feed items
for (int i = 0; i < 10; i++)
{
XmlNode xmlNode;
xmlNode = xmlNodeList.Item(i).SelectSingleNode("title");
if (xmlNode != null)
{
RowValues[0] = xmlNode.InnerText;
}
else
{
RowValues[0] = "";
}
xmlNode = xmlNodeList.Item(i).SelectSingleNode("description");
if (xmlNode != null)
{
RowValues[1] = xmlNode.InnerText;
}
else
{
RowValues[1] = "";
}
// creating datarow and add it to the datatable
DataRow dRow;
dRow = dtable.Rows.Add(RowValues);
dtable.AcceptChanges();
}
// finally bind the grid....
grvRSS.DataSource = dtable;
grvRSS.DataBind();
来源:-
Source:--
<asp:GridView ID="grvRSS" runat="server" CellPadding="4"
EnableModelValidation="True" ForeColor="#333333" GridLines="None"
Width="645px" Height="790px" AutoGenerateColumns="False" >
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField HeaderText="Title">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Image">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Image ID="Image1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"
Wrap="False" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center"
Height="744px" Width="620px" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" Width="620px" Wrap="True" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
推荐答案
HI ...当我尝试使用数据源属性...它给了我结果,但是它散布在整个页面上,看起来显然很糟糕...所以我的问题是我如何从整个网页散布来控制gridview的大小.或者,如果您可以提出其他建议的方法,那将是有帮助的.致谢
HI...when i tried using datasource property...it gave me the result but it spread all over the page which obviously looked very bad... so my problem is how can i control the size of gridview from spread all over the webpage. or if u could suggest any other method to do that , it would be helpfull. Thanx and Regards
减少GridView
的height
(根据您的视图),将其设置为100px或200px,而不是790px.
Reduce height
of the GridView
(Based on your view), Make it 100px or 200px instead of 790px.
<asp:gridview id="grvRSS" runat="server" cellpadding="4" xmlns:asp="#unknown">
EnableModelValidation="True" ForeColor="#333333" GridLines="None"
Width="645px" Height="100px" AutoGenerateColumns="False" ></asp:gridview>
并在Gridview中使用分页(是的,不要一次加载大量记录).
ASP.NET 2.0的GridView示例:对GridView的数据进行分页和排序 [ ^ ]
在GridView中分页大量数据 [ ^ ]
And use paging in Gridview(Yes, don''t load tons of records at a time).
GridView Examples for ASP.NET 2.0: Paging and Sorting the GridView''s Data[^]
Paging tons of data in GridView[^]
这篇关于如何使用TemplateField将DataTable绑定到Gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!