Hi ilanocf,
Check this example. Now please take its reference and correct your code.
Database
CREATE TABLE [dbo].[YoutubeVideos](
[VideoId] [int] NOT NULL,
[VideoUrl] [nvarchar](1000) NOT NULL,
CONSTRAINT [PK_YoutubeVideos] PRIMARY KEY CLUSTERED
(
[VideoId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
INSERT INTO [YoutubeVideos]
SELECT 1, 'https://www.youtube.com/watch?v=uL4eoxZ4Y8E'
UNION ALL
SELECT 2, 'https://www.youtube.com/watch?v=PmvmtC8lDA4'
HTML
C#
<asp:DataGrid ID="DataGrid1" runat="server" AutoGenerateColumns="False" CssClass="table">
<AlternatingItemStyle CssClass="gridAlternate" />
<Columns>
<asp:BoundColumn DataField="VideoId" HeaderText="ID" Visible="False"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="VÍDEO">
<ItemTemplate>
<iframe id="video" width="200" height="200" frameborder="0" allowfullscreen
src='<%# "https://www.youtube.com/embed/" + DataBinder.Eval(Container, "DataItem.VideoUrl").ToString().Split(new string[] {"v="}, StringSplitOptions.None)[1] %>'></iframe>
</ItemTemplate>
</asp:TemplateColumn>
<asp:ButtonColumn CommandName="Edit" HeaderText="EDITE" Text="<img src="../imagens/BtnEdita.png" border="0">">
<HeaderStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Center" />
<ItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Center" />
</asp:ButtonColumn>
<asp:ButtonColumn CommandName="Delete" HeaderText="DELETE" Text="<img src="../imagens/BtnExclui.png" border="0">">
<HeaderStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Center" />
<ItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Center" />
</asp:ButtonColumn>
</Columns>
<HeaderStyle CssClass="bg-success" />
</asp:DataGrid>
VB.Net
<asp:DataGrid ID="DataGrid1" runat="server" AutoGenerateColumns="False" CssClass="table">
<AlternatingItemStyle CssClass="gridAlternate" />
<Columns>
<asp:BoundColumn DataField="VideoId" HeaderText="ID" Visible="False"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="VÍDEO">
<ItemTemplate>
<iframe id="video" width="200" height="200" frameborder="0" allowfullscreen
src='<%# "https://www.youtube.com/embed/" & DataBinder.Eval(Container, "DataItem.VideoUrl").ToString().Split(New String() {"v="}, StringSplitOptions.None)(1) %>'></iframe>
</ItemTemplate>
</asp:TemplateColumn>
<asp:ButtonColumn CommandName="Edit" HeaderText="EDITE" Text="<img src="../imagens/BtnEdita.png" border="0">">
<HeaderStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Center" />
<ItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Center" />
</asp:ButtonColumn>
<asp:ButtonColumn CommandName="Delete" HeaderText="DELETE" Text="<img src="../imagens/BtnExclui.png" border="0">">
<HeaderStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Center" />
<ItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Center" />
</asp:ButtonColumn>
</Columns>
<HeaderStyle CssClass="bg-success" />
</asp:DataGrid>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT VideoId, VideoUrl FROM YoutubeVideos", con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
this.DataGrid1.DataSource = dt;
this.DataGrid1.DataBind();
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindGrid()
End If
End Sub
Private Sub BindGrid()
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT VideoId, VideoUrl FROM YoutubeVideos", con)
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
Me.DataGrid1.DataSource = dt
Me.DataGrid1.DataBind()
End Using
End Using
End Using
End Sub
Screenshot