Problem with GridView

Joined
Jan 5, 2011
Messages
2
Reaction score
0
Hi,

I have a problem with dipslaying the GridView. If I use the code as it is now( the code below) I can not see the inserted data in the GridView which was empty before inserting, neither the data in the GridView if the source/table is not empty while loading the page. I have an empty GridView, and I can see the inserted data only in Server Explorer. Here is the code:


<script runat="server">

protected void Page_Load(object sender, EventArgs e)
{
SqlConnection liniiConnection = new SqlConnection();
DataSet liniiDataSet = new DataSet();
SqlDataAdapter liniiSqlDataAdapter = new SqlDataAdapter();

liniiConnection.ConnectionString = SqlDataSource1.ConnectionString;
liniiSqlDataAdapter.SelectCommand = new
SqlCommand(SqlDataSource1.SelectCommand, liniiConnection);
liniiSqlDataAdapter.Fill(liniiDataSet);


if (!IsPostBack)
{
if (liniiDataSet.Tables[0].Rows.Count == 0)
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("LinijaID", typeof(int)));
dt.Columns.Add(new DataColumn("Ime", typeof(string)));

DataRow dr = dt.NewRow();
dr["LinijaID"] = Convert.ToInt32(txtLinijaId.Text);
dr["Ime"] = "MZT";
dt.Rows.Add(dr);
GridView1.DataSource = dt;
GridView1.DataBind();
ViewState.Add("key", "1");
}
else
{
ViewState.Add("key", "0");
GridView1.DataSource = SqlDataSource1;
GridView1.DataBind();
}
}
}



protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (ViewState["key"].ToString() == "1")
{
GridViewRow row = e.Row;
if (row.RowType != DataControlRowType.DataRow)
return;

DropDownList ddl = new DropDownList();

ddl.ID = row.RowIndex.ToString();
for (Int32 i = 0; i < 5; i++)
ddl.Items.Add(new ListItem(i.ToString(), "0", true));
row.Cells[1].Controls.Add(ddl);
}
}



protected void Button1_Click(object sender, EventArgs e)

{
TextBox LinijaId = txtLinijaId as TextBox;



//TextBox LinijaId = GridView1.FooterRow.FindControl("LinijaIdTextBox") as TextBox;

DropDownList Ime = GridView1.FooterRow.FindControl("ImeDropDownList") as DropDownList;

SqlDataSource1.InsertParameters["LinijaId"].DefaultValue = LinijaId.Text;
//SqlDataSource1.InsertParameters["LinijaId"].DefaultValue = txtLinijaId.Text;

SqlDataSource1.InsertParameters["Ime"].DefaultValue = Ime.SelectedValue;


SqlDataSource1.Insert();
}


</script>



<head id="Head1" runat="server">

<%--<Untitled Page</title>--%>

</head>

<body>

<form id="form1" runat="server">

<div>



<asp:TextBox ID="txtLinijaId" runat="server" CausesValidation="True"
AutoCompleteType="DisplayName">10</asp:TextBox>
<asp:Label ID="Label7" runat="server" Text="Ime na linija"></asp:Label>
<br />
<asp:TextBox ID="txtLineList" runat="server"></asp:TextBox>
<asp:Label ID="Label8" runat="server" Text="Lista"></asp:Label>
<br />
<asp:TextBox ID="txtGeoVal" runat="server"></asp:TextBox>
<asp:Label ID="Label9" runat="server" Text="GeoVal"></asp:Label>
<br />
<asp:SqlDataSource ID="SqlDataSource4" runat="server" InsertCommand="INSERT Linii ( LinijaIme, LineList, GeoVal)
VALUES ( @Ime, @LineList, @GeoVal)">
<InsertParameters>
<asp:ControlParameter ControlID="txtLinijaId" Name="Ime" PropertyName="Text" />
<asp:ControlParameter ControlID="txtLineList" Name="LineList"
PropertyName="Text" />
<asp:ControlParameter ControlID="txtGeoVal" Name="GeoVal" PropertyName="Text" />
</InsertParameters>
</asp:SqlDataSource>
<asp:ValidationSummary ID="textBoxValidationSummary1" runat="server"
HeaderText="Ovie greski se najdeni pri vnesot" />

<br />

<asp:Label ID="Label5" runat="server" Text="Vkupno postojki na ovaa linija:"></asp:Label>

<asp:Label ID="Label6" runat="server"></asp:Label>
<br />
<br />

<asp:GridView ID="GridView1" Runat="server" DataKeyNames="LinijaID"

AutoGenerateColumns="False" ShowFooter="True" CellSpacing="1" AllowPaging="True"
onrowcreated="GridView1_RowCreated" >

<Columns>

<asp:TemplateField HeaderText="LinijaId">

<ItemTemplate>

<asp:Label ID="LinijaIDLabel" Runat="Server"><%# Eval("LinijaID") %></asp:Label>

</ItemTemplate>

<FooterTemplate>

<asp:TextBox ID="LinijaIDTextBox" Runat="server"></asp:TextBox>

</FooterTemplate>

</asp:TemplateField>


<asp:TemplateField HeaderText="PIme">

<FooterTemplate>

<asp:DropDownList ID="ImeDropDownList" Runat="server" DataSourceID="SqlDataSource2" DataTextField="Ime" DataValueField="Ime">

</asp:DropDownList>

<asp:SqlDataSource ID="SqlDataSource2" Runat="server" SelectCommand="SELECT DISTINCT [Ime] FROM [Postojki]"

ConnectionString="<%$ ConnectionStrings:ConnString%>">

</asp:SqlDataSource>

<asp:Button ID="Button1" Runat="server" Text="Add" OnClick="Button1_Click" />

</FooterTemplate>

<ItemTemplate>

<asp:DropDownList ID="ImeDropDown" SelectedValue='<%# Bind("Ime") %>' Runat="Server" DataSourceID="SqlDataSource3" DataTextField="Ime" DataValueField="Ime" ></asp:DropDownList>

<asp:SqlDataSource ID="SqlDataSource3" Runat="server" SelectCommand="SELECT DISTINCT [Ime] FROM [Postojki]"

ConnectionString="<%$ ConnectionStrings:ConnString%>" EnableCaching="True">

</asp:SqlDataSource>

</ItemTemplate>

</asp:TemplateField>

</Columns>

</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" Runat="server"

InsertCommand="INSERT INTO [LinPost2] ([LinijaID], [Ime]) VALUES (@LinijaID, @Ime)"

SelectCommand="declare @LinijaID int
SELECT [LinijaID], [Ime] FROM [LinPost2] where (LinijaID=@LinijaID) ORDER BY [LinijaID] ASC"

ConnectionString="<%$ ConnectionStrings:ConnString %>">

<SelectParameters>
<asp:ControlParameter ControlID="txtLinijaId" DefaultValue="" Name="@LinijaID"
PropertyName="Text" />
<asp:parameter Name="LinijaID" />
</SelectParameters>

<DeleteParameters>

<asp:parameter Type="String" Name="LinijaID"></asp:parameter>

</DeleteParameters>

<UpdateParameters>

<asp:parameter Type="String" Name="LinijaID"></asp:parameter>



<asp:parameter Type="String" Name="Ime"></asp:parameter>

</UpdateParameters>

<InsertParameters>

<asp:ControlParameter ControlID="txtLinijaId" Name="LinijaID"
PropertyName="Text" Type="String" />



<asp:parameter Type="String" Name="Ime"></asp:parameter>

</InsertParameters>

</asp:SqlDataSource>


</div>

</form>

</body>

</html>

And if I don't write the page_load and GridView1_RowDataBound events, I have the GridView with all data displayed as well as the data I am inserting, but I can't see the GridView when it is empty, when the table/data source is empty while loading the page.

Could anyone help me please?
I am eagerly waiting for your respones. Thank you!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top