I have a gridview control in my ASP.NET web form. I the past I have used the same code to fill a gridview with data in a Windows Forms App.
Here is my code:
var sql = new SQL_Statements();
var stringSql = "select col1, col2, col3, col4 from table1 where col5=" + stringPO_NUM;
var sql_ds = sql.SelectFromDB(stringSql);
int DataIndex;
if (sql_ds.Tables["CurData"].Rows.Count > 0)
for (DataIndex = 0; DataIndex <= sql_ds.Tables["CurData"].Rows.Count - 1; DataIndex++)
{
sql_ds.Tables["CurData"].Rows[DataIndex]["col4"].ToString();
//Fill the datagridview with the data that was just pulled from SQL_Statements
GV_POI.DataSource = sql_ds;
GV_POI.DataMember = "CurData";
}
Here is the code for the SelectFromDB:
public DataSet SelectFromDB(String Sql)
{
var functionReturnValue = default(DataSet);
var Class_Connection = new SQL_Connection();
Class_Connection.cnn.Close(); //Let's close the connection, just in case it is open
Class_Connection.cnn.Open();
var myDataAdaptor = new SqlDataAdapter(Sql, Class_Connection.cnn);
var myDataset = new DataSet();
try
{
myDataAdaptor.SelectCommand.Connection = Class_Connection.cnn;
myDataAdaptor.SelectCommand.CommandText = Sql;
myDataAdaptor.SelectCommand.CommandType = CommandType.Text;
myDataAdaptor.Fill(myDataset, "CurData");
functionReturnValue = myDataset;
}
catch (Exception ex)
{
Console.WriteLine(ex);
Class_Connection.cnn.Close();
}
return functionReturnValue;
Class_Connection.cnn.Close();
}
Here is the code for the GridView in the ASPX page:
<asp:GridView ID="GV_POI" runat="server" AutoGenerateColumns="False">
</asp:GridView>
I am not sure what I am doing wrong. When the page loads, the gridview is blank. I checked the code in the debugger, and code is firing. Can someone please tell me what I am doing wrong?
If you are getting all data in dataset "sql_ds" then there is no need of for loop , you can do as below
GV_POI.DataSource = sql_ds.Tables[0];
GV_POI.DataBind();
and also either make AutoGenerateColumns="True" or use BoundField or TemplateField.
Example:
<asp:GridView ID="gvTest" runat="server" AutoGenerateColumns="False" GridLines="Vertical"
OnRowEditing="gvTests_RowEditing" AllowPaging="True" PageSize="20"
OnRowDataBound="gvTest_RowDataBound">
<Columns>
<asp:BoundField HeaderText="ID" DataField="ID" ItemStyle-CssClass="hidecss" HeaderStyle-CssClass="hidecss" >
<ItemStyle Width="80px" HorizontalAlign="Left" />
<HeaderStyle Width="80px" Wrap="False" />
</asp:BoundField>
<asp:BoundField HeaderText="Name" DataField="Name">
<ItemStyle Width="200px" HorizontalAlign="Left" />
<HeaderStyle Width="200px" Wrap="False" />
</asp:BoundField>
<asp:BoundField HeaderText="Description" DataField="Description">
<ItemStyle Width="200px" HorizontalAlign="Left" />
<HeaderStyle Width="200px" Wrap="False" />
</asp:BoundField>
<asp:CommandField ShowEditButton="true" EditImageUrl="~/images/edit.png" ButtonType="Image">
<HeaderStyle Width="20px" />
<ItemStyle Width="20px" />
</asp:CommandField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="btnDelete" runat="server" CommandName="Delete"
ImageUrl="~/images/delete.png" AlternateText="Delete"
ToolTip="Click to Delete"></asp:ImageButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle CssClass="HeaderCss" />
<PagerStyle CssClass="PagerCss" />
<AlternatingRowStyle CssClass="AltRowCss" />
<EditRowStyle CssClass="EditRowCss" />
</asp:GridView>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With